练习5.14

来源:互联网 发布:java并编程实战百度云 编辑:程序博客网 时间:2024/06/05 16:18
编写一段程序,从标准输入中读取若干string对象并查找连续重复出现的单词。所谓连续重复出现的意思是:一个单词后面紧跟着这个单词本身。要求记录连续重复出现的最大次数以及对应的单词。如果这样的单词存在,输出重复出现的最大次数:如果不存在,输出起跳信息说明任何单词都没有连续出现过。例如,如果输入是
 how now now now Brown cow cow

那么输出应该表明单词now连续输出了3次



#include <iostream>using namespace std;int main(){    string str;    string str1;    int count;    int max=1;    if(cin>>str){        str1=str;    }    while(cin>>str){        if(str==str1){            count++;            if(max<count){                max=count;            }        }        else{            str1=str;            count=1;        }        }    if(max>1)    cout<<max<<endl;    else    cout<<"无重复"<<endl;    return 0;}

0 0
原创粉丝点击