【笔试】华为实习生 上机
Q1
input:A row of figures separated by commas
output:A row of figures separated by commas according to the original order.Each number=the original value*the nth time appeared
My answer:
1 #include<iostream> 2 #include<string> 3 #include<array> 4 #include<cmath> 5 using namespace std; 6 7 8 int main(){ 9 10 //输入获取 每个输入字符存入字符串inputString 11 string inputString; 12 cin>>inputString; 13 int stringLen=inputString.length(); 14 15 //剔除逗号,逗号之间的数存入数组numArray的每个元素里 16 int *numArray=new int[100](); 17 int numArrayLen=0; 18 19 int i=inputString.find(","); 20 for(int j=0,jj=i-1;j<i,jj>=0;j++,jj--){ 21 numArray[0]+=(inputString[j]-48)*pow(10.0,jj); 22 } 23 24 inputString[i]=' '; 25 int ii=inputString.find(","); 26 27 while(ii!=string::npos){ 28 numArrayLen++; 29 for(int j=i+1,jj=ii-i-2;j<ii,jj>=0;j++,jj--){ 30 numArray[numArrayLen]+=(inputString[j]-48)*pow(10.0,jj); 31 } 32 i=ii; 33 inputString[ii]=' '; 34 ii=inputString.find(","); 35 } 36 37 numArrayLen++; 38 for(int j=i+1,jj=stringLen-i-2;j<stringLen,jj>=0;j++,jj--){ 39 numArray[numArrayLen]+=(inputString[j]-48)*pow(10.0,jj); 40 } 41 42 //计算每个数的出现次数,并更新数组 43 int *numCount=new int[1000](); 44 for(int j=0;j<=numArrayLen;j++){ 45 numCount[numArray[j]]++; 46 numArray[j]*=numCount[numArray[j]]; 47 } 48 49 //按照要求的格式输出 50 for(int j=0;j<numArrayLen;j++)cout<<numArray[j]<<","; 51 cout<<numArray[numArrayLen]; 52 53 }
I did not realize my knowledge was so weak until the test.There are a lot of c++ function that I did not know before or didn't know how to use it well.Consequently,I must still keep fighting harder.By the way ,coding them strengthens my debug aility.F10~~
Ps:It is my first time to write something here in English.
On one hand ,to record a noob who hope to grow into a female geeker on programming.On the other hand,to train my English writing.
Please enlighten.