65,206
社区成员
发帖
与我相关
我的任务
分享
int main() {
cout << "enter some words" << endl;
string preword, curword, maxword;//前一个单词,当前输入的单词,重复出现的单词
int curtime, maxtime;//当前出现的次数,最大重复的次数
int first = 1;
while(cin >> curword) {
if(first == 1){
curtime = 1;
maxword = curword;
maxtime = 1;
preword = curword;
first = 0;
} else if(curword == preword){
++curtime;
if(curtime > maxtime){
maxtime = curtime;
maxword = curword;
}
} else {
curtime = 1;
preword = curword;
}
}
if(maxtime == 1)
cout<<"no repeat"<<endl;
else
cout<<"the repeat word is "<<maxword<<", "<< maxtime << " times." << endl;
return 0;
}while(cin>>curword)这个难道不是没循环一次,到判断是否跳出循环时,又等待输入,这个表达式的直不会为0吧while(cin>>curword)
这货是什么意思? 判断条件是个输入语句?#include<iostream>
#include<string>
using namespace std;
void main()
{
cout<<"enter some words"<<endl;
string preword="",curword="",maxword="";//前一个单词,当前输入的单词,重复出现的单词
int curtime=0,maxtime=1;//当前出现的次数,最大重复的次数
do
{
cin>>curword;
cout<<curword<<endl;
if(curword==preword)
++curtime;
else
curtime=1;
if(curtime>maxtime)
{
maxtime=curtime;
maxword=preword;
cout<<maxword<<endl;
}
preword=curword;
}while(cin.get()!='\n');
if(maxtime!=1)//如果有重复的单词
cout<<"the repeat word is"<<maxword<<"the time is"<<maxtime<<endl;
else//如果没有重复的单词
cout<<"there is no repeat times"<<endl;
}