感觉可能是我输入有误

lrd408 2008-10-23 04:58:04
#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;
}


这个题目,也是c++ primer 上的,我不会编,看了答案,就是这样,可你输入we we we ctrl+z ctrl+z
ctrl+z
结果:There is no repeated word!
今天挺郁闷两道题都有问题
题目:
编写一个小程序,从标准输入读入一系列 string 对象,寻找连续重复出现的单词。程序应该找出满足以下条件的单词的输入位置:该单词的后面紧跟着再次出现自己本身。跟踪重复次数最多的单词及其重复次数。输出重复次数的最大值,若没有单词重复则输出说明信息
...全文
136 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lrd408 2008-10-23
  • 打赏
  • 举报
回复
明天再看吧。我有些累了
Vicent20138888 2008-10-23
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>

using namespace std;

int main()
{
int currCnt = 1, maxCnt = 1;

string repWord,preWord,currWord;

cout<<"\nplease Enter words to count the repeated words:"<<endl;

while( cin >> currWord ) // 循环输入
{
if( currWord == preWord ) // 相同时计数
{
++currCnt; // 记录此时相同的个数
}

if( currCnt > maxCnt ) // 满足条件一直更新 maxCnt 和 repWord
{
maxCnt = currCnt;
repWord = preWord;
}

if( preWord != currWord ) // 遇到不同的字符串, 重新计数, 更新 preWord
{
currCnt = 1;
preWord = currWord;
}
}

if( maxCnt > 1 ) // 有重复的字符串则输出
{
cout<< endl << repWord << " repeats " << maxCnt << " times!" << endl;
}
else
{
cout<< "\nThere is no repeated word!" <<endl;
}

return 0; // 程序执行结束
}

/*
运行一下,看行不??

你的问题是 更新 maxCnt 的条件??

如果 输入 we we we, maxCnt 不会被赋新值的..

只能每次都要测试一下..., 应为 while( cin >> currWord ),
不知何时会结束...

吃饭去了, 饿死了...
*/
figo1885 2008-10-23
  • 打赏
  • 举报
回复
就是你的逻辑问题,只要你输入三个或者四个,然后输入个不同的就可以了
就像楼上两位说的一样。
we we we de ctrl+z
就可以了
lzr4304061988012 2008-10-23
  • 打赏
  • 举报
回复

#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;
}
ysuliu 2008-10-23
  • 打赏
  • 举报
回复
你的逻辑有问题,仔细看看就知道了

第一个we
preWord : we
curWord : we
currCnt : 0
maxCnt : 1

第二个we
preWord : we
curWord : we
currCnt : 1
maxCnt : 1

第三个we
preWord : we
curWord : we
currCnt : 2
maxCnt : 1

然后就是c+z, maxCnt根本就没变化。。
Dan_M 2008-10-23
  • 打赏
  • 举报
回复
这个程序应该是有不同的输入时才判断重复结束


试试
we we we de ctrl+z
Dan_M 2008-10-23
  • 打赏
  • 举报
回复
we we we 如果输入是一样的话maxCnt就永远是1

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧