【 2013华为杯编程大赛成都第三组前两题试题及答案】
2013-09-12 16:41:24
題目描述
某省會城市街道縱橫交錯,為了監控路燈的運行狀況,每條街道使用一個數字字符串標識該街道上所有路燈的運行狀況。
假設路燈只有如下3種狀態(分別用數字0, 1, 2標識,一盞路燈只對應其中一種狀態):
0 標識路燈熄滅;
1 標識路燈開啟;
2 標識路燈故障;
請根據輸入的字符串,找出該街道上連續的處于相同狀態的路燈的最大個數。若兩種狀態的路燈數量相同,則返回最先出現的路燈狀態。
輸入
街道上連續的路燈組成的狀態字符串。字符串中只包含數字,每個路燈的狀態為0,1,2中的一種狀態。如“1101”代表4盞路燈,第3盞路燈為熄滅狀態,其它3盞為開啟狀態。
輸出
連續為相同狀態的路燈的最大數量;
上述路燈的狀態;
要求:先輸出數量,再輸出狀態,兩個整數間采用一個空格間隔。如輸出:
53 2
樣例輸入
112200111
樣例輸出
3 1
提示
OK
地區
成都研究所
題目描述
由實部和虛部組成,形如(a,bi)這樣的數,稱為復數。通信系統中,通常用32bit數來表示復數(高16bit表示實部,低16bit表示虛部),如整數524295(16進制為0x00080007)所代表的復數,實部為0x0008,虛部為0x0007。
有別于實數運算,復數加、減、乘、除運算定義如下:
復數加公式:(a,bi) + (c,di) = (a + c),(b + d)i
復數減公式:(a,bi) + (c,di) = (a - c),(b - d)i
復數乘公式:(a,bi) * (c,di) = (ac - bd),(ad + bc)i
復數除公式:(a,bi) / N = (a/N),(b/N)i
題目要求,輸入N個復數,計算這個N個復數的平均值,復數Avg = (復數1*復數2 + 復數3*復數4 + … + 復數N-1*復數N) / N。
復數加、復數減、復數乘、復數除的結果仍然為復數,實部和虛部均為16bit有符號數,計算過程中,當結果大于32767(0x7fff)時,輸出32767;當計算結果小于-32768(0x8000)時,輸出-32768。
輸入
輸入共計兩行
有別于實數運算,復數加減乘除運算定義如下第一行包含1個整數,表示輸入復數個數N(N為偶數,N不大于1000)
第一行包含1個整數,表示輸入復數個數N(N為偶數,N不大于1000)
輸出
經計算得到的復數的平均值。
樣例輸入
4
262149,393223,524297,655371
262149 = 0x40005,393223 = 0x6007,524297 = 0x8009,655371 = 0xA00B
[(4+5i)*(6+7i) + (8+9i)*(10+11i)]/4 = (-28+236i)/4 = -7+59i
-7對應的16位有符號數為65529,59對應的16位有符號數為59,對應的32位有符號數為int((-7+65536)*65536 + 59 ) = -458693
?
4,
-196613,393223,-458761,655371
[(-4-5i)*(6+7i) + (-8-9i)*(10+11i)]/4 = (28-236i)/4 = 7-59i,對應的32位有符號數為:
int(7*65536 + (65536-59 ) = 524229
對應的4個數為:-196613,393223,-458761,655371
?
樣例輸出
-458693
提示
無
地區
成都研究所
?
注意幾點:
?
?
代碼:
1 #include <iostream> 2 #include <cassert> 3 using namespace std; 4 5 const size_t SIZE = 1000; 6 7 void CheckValidInput(const char *pStrState) 8 { 9 assert(pStrState != NULL); 10 char *pCur = (char *)pStrState; 11 12 while (*pCur) 13 { 14 assert(*pCur >= '0' && *pCur <= '2'); 15 ++pCur; 16 } 17 } 18 19 void CountLamp(const char *pStrState,char &state,size_t &count) 20 { 21 CheckValidInput(pStrState); 22 23 count = 0; 24 state = '\0'; 25 26 char *pCur = (char *)pStrState; 27 char curChar = '\0'; 28 size_t curTimes = 0; 29 30 while (*pCur) //對非法狀態的處理??? 31 { 32 curTimes = 0; 33 curChar = *pCur; 34 35 while (*pCur && *pCur == curChar) 36 { 37 ++curTimes; 38 ++pCur; 39 } 40 41 if (curTimes > count) 42 { 43 state = curChar; 44 count = curTimes; 45 } 46 } 47 } 48 49 const size_t BitWidth = 16; 50 const int Max = 65535; 51 const int MaxPositiveNum = 32767; //2^15 - 1,是2與15異或的結果減1 52 const int MinNegativeNum = -32768; 53 54 //需要溢出處理,直接定義為short類型不能按照 55 //當結果大于32767(0x7fff)時,輸出32767;當計算結果小于-32768(0x8000)時,輸出-32768 56 //的方式處理 57 short HandleOverFlow(int num) //返回值類型 58 { 59 if (num < MinNegativeNum) 60 { 61 return MinNegativeNum; 62 } 63 else if (num > MaxPositiveNum) 64 { 65 return MaxPositiveNum; 66 } 67 68 return num; 69 } 70 71 int ComplexAdd(const int compNum1,const int compNum2) 72 { 73 short ar = compNum1 >> BitWidth; 74 short ai = compNum1 & Max; 75 short br = compNum2 >> BitWidth; 76 short bi = compNum2 & Max; 77 78 cout<<"("<<ar<<" + j*"<<ai<<") + ("<<br<<" + j*"<<bi<<") = "; 79 80 int sumr = ar + br; //需定義為int類型,保證溢出處理時按要求的 81 int sumi = ai + bi; 82 83 sumr = HandleOverFlow(sumr); //HandleOverFlow返回即為short類型的 84 sumi = HandleOverFlow(sumi); 85 86 cout<<sumr<<" + j* "<<sumi<<endl; 87 return ((sumr << BitWidth) | (sumi & Max) ); 88 } 89 90 int ComplexMult(const int compNum1,const int compNum2) 91 { 92 short ar = compNum1 >> BitWidth; //定義為short類型即可 93 short ai = compNum1 & Max; 94 short br = compNum2 >> BitWidth; 95 short bi = compNum2 & Max; 96 97 cout<<"("<<ar<<" + j*"<<ai<<") * ("<<br<<" + j*"<<bi<<") = "; 98 99 int multr = ar * br - ai * bi; //要定義為int類型,而非short類型,否則在溢出時,不能按照要求的溢出處理輸出 100 int multi = ar * bi + ai * br; 101 102 multr = HandleOverFlow(multr); 103 multi = HandleOverFlow(multi); 104 105 cout<<multr<<" + j* "<<multi<<endl; 106 107 return ((multr << BitWidth) | (multi & Max) ); 108 } 109 110 int ComplexDivideN(const int compNum1,const int N) 111 { 112 short ar = compNum1 >> BitWidth; 113 short ai = compNum1 & Max; 114 115 cout<<"("<<ar<<" + j*"<<ai<<") / "<<N<<" = "; 116 117 int divider = (short)ar / N; //divider、dividei定義為int、short都可 118 int dividei = (short)ai / N; 119 120 cout<<"("<<divider<<" + j* "<<dividei<<")"<<endl; 121 122 return ( (divider << BitWidth) | (dividei & Max) ); 123 } 124 125 int ComplexMultAddDivide(const int *compArray,const int n) 126 { 127 assert(compArray != NULL); 128 assert(n > 0 && n <= 1000 && (n % 2 == 0)); 129 130 int index; 131 long long sum = 0; 132 133 for (index = 0;index < n;index += 2) 134 { 135 sum = ComplexAdd( sum,ComplexMult(compArray[index],compArray[index + 1]) ); 136 } 137 138 return ComplexDivideN(sum,n); 139 } 140 141 void TestDriver() 142 { 143 //char *pStrState = "112200111"; 144 //char *pStrState = "112200"; 145 //char *pStrState = "12200"; 146 //char *pStrState = "1200"; 147 /*char *pStrState = ""; 148 char state = '\0'; 149 size_t count = 0; 150 CountLamp(pStrState,state,count); 151 152 cout<<"the state appears most is : "<<state<<endl; 153 cout<<"the times is : "<<count<<endl;*/ 154 155 156 //int compArray[SIZE] = {1,0xff00ff}; 157 int compArray[SIZE] = { 262149,393223,524297,655371 }; 158 //int compArray[SIZE] = { -196613,393223,-458761,655371 }; 159 //int compArray[SIZE] = { 32767,32767,-32767,32767 }; //溢出測試 160 //int compArray[SIZE] = {524297,655371}; 161 int n = 4; 162 int average = 0; 163 average = ComplexMultAddDivide(compArray,n); 164 165 cout<<"the average is : "<<average<<endl; 166 } 167 168 int main() 169 { 170 TestDriver(); 171 return 0; 172 }運行結果:
(4 + j*5) * (6 + j*7) = -11 + j* 58 (0 + j*0) + (-11 + j*58) = -11 + j* 58 (8 + j*9) * (10 + j*11) = -19 + j* 178 (-11 + j*58) + (-19 + j*178) = -30 + j* 236 (-30 + j*236) / 4 = (-7 + j* 59) the average is : -458693 請按任意鍵繼續. . .?
總結
以上是生活随笔為你收集整理的【 2013华为杯编程大赛成都第三组前两题试题及答案】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL优化准则
- 下一篇: 解决Office系列安装不上的办法