初学者一起看吧。自己学习中遇到的。已经解决。
2009-06-18 21:04
363 查看
//从标准输入读入一系列string对象,寻找连续重复出现的单词 //输出重复次数的最大值,若没有单词重复则输出说明信息 #include<iostream> #include<string> #include<cstdlib> using namespace std; int main(){ string preWord,currWord; string repWord; int currCnt=0,maxCnt=1; cout<<"Enter some words:"<<endl; while(cin>>currWord){ if(preWord==currWord) ++currCnt;//1 else{ if(currCnt>maxCnt){ maxCnt=currCnt; repWord=preWord; } currCnt=1; } preWord=currWord; } if(maxCnt!=1) cout<<'"'<<repWord<<'"' <<"repeated for"<<maxCnt <<"times:"<<endl; else cout<<"there is none of...."<<endl; system("pause"); } ////此程序AAAAAAA级经典。且看 1语句 //如果输入ni ni ta ta ta wo wo wo wo //联系1语句。输入wo的时候++currCnt一直在循环!根本没机会进行下面的if语句 //也就造成了wo和ta 没有进行比较!所以才会输出错误的结果!!!!!! 最后一次输入是无效的,所以结束循环, 直接退出去,这就导致了不能够对记录进行修改了,这个程序只有在最后一批数据最大的时候才会出问题 //正确代码如下 #include <iostream> #include <string> #include <cstdlib> using namespace std; int main(){ string preWord,currWord; string repWord; int currCnt=0,maxCnt=1; cout <<"Enter some words:" <<endl; while(cin>>currWord) { ++currCnt; if(preWord!=currWord) { if(currCnt>maxCnt) { maxCnt=currCnt; repWord=currWord; ///////////////////// } currCnt=1; } preWord=currWord; } if(maxCnt!=1) cout <<'"' <<repWord <<'"' <<"repeated for" <<maxCnt<<"times:" <<endl; else cout <<"there is none of...." <<endl; system("pause"); } 或者 #include <iostream> #include<cstdlib> #include <map> using namespace std; int main() { map<string,int> m; string name; while (cin>>name) { m[name]++; } int max=-1; string maxName; for (map<string,int>::const_iterator it = m.begin(); it != m.end(); it++) { if (it->second > max) { maxName = it->first; max = it->second; } } cout<<maxName<<":"<<max<<endl; system("pause"); }
相关文章推荐
- 单位的局域网遇到了这样的问题,已经解决了,大家学习下..
- 自己学习Struts2过程中遇到的问题及解决办法(第一天)
- 自己开始学习到遇到问题及解决方法的记录
- 再次解决以前遇到而且已经解决的问题
- Linux mmap学习及遇到的问题解决
- 大二学生一枚 写下自己学51和PCB的感想 与各位刚开始学习的盆友共勉 一起加油
- ASP.NET遇到问题,“对象不能从 DBNull 转换为其他类型”,记录一下~问题已经解决,谢谢大家~
- Idhttp 学习中遇到的问题 与 解决方法
- 在自己学习stuts是遇到问题后总结的经验。
- 自己在学习Oracle10g中遇到的一系列问题
- MFC有时候也不是那样的神,关键时候还是自己写的好,今天早上遇到的状况下面是解决
- 一起艳恶学习开发遇到的坑(一)
- 初学者使用vs2013编译c语言程序遇到问题及解决办法。
- 学习两天Vue.js遇到的问题及解决办法。
- win7下电脑疑难杂症解决集合(自己遇到的情况)
- 使用Hibernate过程中所遇到的问题已经解决方法
- 记录自己集成支付宝遇到的问题及解决方法
- cmake 学习之路 记录中间所遇到过的所有错误与问题的解决 <三>
- cmake 学习之路 记录中间所遇到过的所有错误与问题的解决 <四>
- Python学习笔记:学习爬虫时遇到的问题TypeError: cannot use a string pattern on a bytes-like object 与解决办法