统计连续出现的重复最多的同一单词的个数
#include<iostream>#include<string>using namespace std;int main(){ string curstr,prestr,most_time_str; int curtimes=0,maxtimes=1; while(cin>>curstr) { if(curstr==prestr) { ++curtimes; } else { if(curtimes>maxtimes) { maxtimes=curtimes; most_time_str=curstr; } curtimes=1; } prestr=curstr; } if(maxtimes!=1) cout<<"maxtimes="<<maxtimes<<endl;}#include <iostream>#include <string>using namespace std;int staticsNum(char *pStr){ int length = (int)strlen(pStr); int count = 1; int max = 0; for(int i=0;i<length-1;i++) { if(pStr[i]==pStr[i+1]) { count ++; if(i+2 == length) max = count; } else { if(max<count) max = count; count = 1; } } return max;}int main(){ for(int i = 0; i < 50; i++) { string str; cin>>str; char *pt = (char *)str.c_str(); int MaxNum = staticsNum(pt); cout<<MaxNum<<endl; } return 0;}
[解决办法]
循环后面再做一次比较不就ok??