Main topic:
Enter a string stream with numbers and non-numbers, non-digits separated by numbers, to find out the number that has the most occurrences.
Ideas:
First replace all non-digits with a uniform character, and then find the number, you need to determine whether the next character is a number, and then save the number into the HashMap, the presence of the number, HashMap the value+1, and the last output value of the largest number.
Code:
1#include"iostream"2#include"Map"3#include"Vector"4#include"algorithm"5 #defineMAX 100006 using namespacestd;7 8 CharC[max];9 BOOLtag;Tenmap<int,int>M,msort; One A intMain () - { - while(Cin >>c) the { -Tag =false; - for(inti =0; I < strlen (c); i++) - { + if(C[i] <'0'|| C[i]>'9') -C[i] ='*'; +cout <<C[i]; A } atcout <<Endl; - for(inti =0, j=0; I < strlen (c); i++) - { - intt=0; - while(C[i] = ='*') -i++; in while(C[i]! ='*'&&c[i]!=' /') - { toTag =true; +T + = C[i]-'0'; - the if(C[i +1] ==' /') * Break; $ Panax Notoginseng if(C[i +1] =='*') -Tag =false; the + if(TAG) AT *=Ten; thei++; + } - if(M.count (t)) $ { $m[t]++; -}Else -M[T] =1; the -J + +;Wuyi } the -map<int,int>::iterator res =M.begin (); Wu for(map<int,int>::iterator iter = M.begin (); Iter!=m.end (); iter++) - { About if(Iter->second > res->second) $res =iter; - - } -cout << Res->first <<Endl; A + m.clear (); the } -System"Pause"); $ the}
2016 Ali School recruit Research and development algorithm problem 9.9