33,311
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<string>
using namespace std;
int main()
{
string word,preword,repword;
int repnum=0,maxnum=1;
while(cin>>word){
if(word==preword)
++repnum; //重复单词计数
else{
if(repnum>maxnum){ //出现重复次数更多的单词
maxnum=repnum;
repword=preword;
}
repnum=1; //计数器置1
}
preword=word;
}
if(maxnum==1){
cout<<"no repeat"<<endl;
}
else
cout<<repword<<" repeat "<<maxnum<<endl;
return 0;
}
if(repnum>maxnum){ //出现重复次数更多的单词
maxnum=repnum;
repword=preword;
}
没有被执行,所以没有赋值,导致最后结果错误