原帖:
(33条消息) C++ primer(第五版) 练习 5.14 个人code_jierandefeng的博客-CSDN博客https://blog.csdn.net/jierandefeng/article/details/38300099
(这博客好老...)
根据评论得知连续输入 "now" 3次 输出是零,问题在于输入Ctrl + Z 后没有执行 while 循环内 else 的语句,只要在结束后重新判断即可。
代码如下:#include#include int main() { std::string str, now, maxStr; unsigned cnt = 0, maxCnt = 0; while(std::cin >> str) { if(str == now) { ++cnt; } else { now = str; if(cnt >= maxCnt) { maxCnt = cnt; maxStr = now; } cnt = 1; } } if(cnt >= maxCnt) { maxCnt = cnt; maxStr = now; } std::cout << '"' << maxStr << '"' << " occurs " << maxCnt << " times." << std::endl; return 0; }
PS:
第一次写 blog ,点个赞再走呗!



