65,211
社区成员
发帖
与我相关
我的任务
分享#include <iostream>
#include <string>
using namespace std;
int main()
{
string repWord,preWord,currWord;
int currCnt=0,maxCnt=1;
cout<<"please Enter words to count the repeated words:"<<endl;
while(cin>>currWord) {
if(currWord==preWord)
++currCnt;
else {
if(currCnt>maxCnt)
{maxCnt=currCnt;
repWord=preWord;
}
currCnt=1;
}
preWord=currWord;
}
if(maxCnt!=1)
cout<<'"'<<repWord<<'"'<<" repeats "<<maxCnt<<" times!"<<endl;
else cout<<"There is no repeated word!"<<endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
string repWord,preWord,currWord;
int currCnt=0,maxCnt=1;
cout<<"please Enter words to count the repeated words:"<<endl;
while(cin>>currWord) {
if(currWord==preWord)
++currCnt;
else {
if(currCnt>maxCnt)
{maxCnt=currCnt;
repWord=preWord;
}
currCnt=1;
}
preWord=currWord;
}
if(currCnt>maxCnt)//遇到结束符时,未执行这个就退出循环了,所以加上这个.
{maxCnt=currCnt;
repWord=preWord;
}
if(maxCnt!=1)
cout<<'"'<<repWord<<'"'<<" repeats "<<maxCnt<<" times!"<<endl;
else cout<<"There is no repeated word!"<<endl;
return 0;
}