您的位置:首页 > 编程语言 > C语言/C++

c++ primer习题6.12答案

2008-04-20 21:03 218 查看
//6_12.cpp
//从标准输入一系列string对象,寻找连续重复出现的单词
//输入重复次数的最大值,若没有单词重复则输入说明信息
#include<iostream>
#include<string>
using namespace std;
int main(){
string Preword,Curword,Repword;
int Ccount=0,Maxcount=0;//当前重复的单词以及重复的最大次数
cout<<"input word"<<endl;
while(cin>>Curword){
if(Curword==Preword)
Ccount++;
else { //当前单词不是前一单词的重复
if(Maxcount<Ccount){
Maxcount=Ccount;
Repword=Preword;
}
Ccount=1;
}
Preword=Curword;
}
if(Maxcount != 1)
cout<<'"'<<Repword<<'"'<<"repeat for"<<Maxcount
<<"times"<<endl;
else cout<<"no repeat word";
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: