这样的题目算难吗?大家做做看怎样 (C++)

theSongInTheMidnight 2009-03-28 07:53:37
编写一个小程序,从标准输入读入一系列string对象,寻找连续重复出现的单词,程序应该找出满足以下条件的单词的输入位置:
该单词的后面跟着再次出现自己本身,跟踪重复次数最多的单词以及重复的次数,输出重复次数的最大值,若没有单词重复,则输出说明信息.
例如:输入是 how, now now now now am am i am am am
则输出应表明"now"这个单词连续出现四次,是重复最多次的单词.


大家觉得这样的题目算难吗???
这是C++ primer 的一道练习题目.我这里有答案.大家可以做做看.
...全文
276 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 I_Younger 的回复:]
12楼的代码有问题!!!
how num num h hh h h h h ni hao main tai haoe le hehe sheng meshi a

结果是Count for string "h" is 4
[/Quote]


没有问题啊.你的那那个hh 和 h 是不一样的啊..连续重复最多的是h ,次数是4,正确.
I_Younger 2009-03-29
  • 打赏
  • 举报
回复
12楼的代码有问题!!!
how num num h hh h h h h ni hao main tai haoe le hehe sheng meshi a

结果是Count for string "h" is 4
javaboy_2008 2009-03-28
  • 打赏
  • 举报
回复
来看看!!
cnStreamlet 2009-03-28
  • 打赏
  • 举报
回复
呀!看错题目,不好意思,顶 12 楼
T技术沙龙 2009-03-28
  • 打赏
  • 举报
回复
运行有误
  • 打赏
  • 举报
回复
晕,16楼怎么连泛型都出来了。
  • 打赏
  • 举报
回复
12楼高手.
简洁
  • 打赏
  • 举报
回复

main()
{
map< string, int> mm;
string str;
while(cin>>str) //可以接收行,也可以多行,不想被getlien限制,结束输入请按q回车
{
if(str=="q")
break;
if(mm.find(str)!=mm.end())
mm[str]++;
else
mm[str]=0;
}
cout<<"input the words you want to check,'q' for quit"<<endl;
str="";
while(cin>>str&&str!="q")
{
if(mm.find(str)!=mm.end())
cout<<str<<": "<<mm[str]+1<<endl;
else
cout<<"No such words"<<endl;
}

return 0;
}
cnStreamlet 2009-03-28
  • 打赏
  • 举报
回复

#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <algorithm>

using namespace std;

inline bool compare(const pair<string, int> &lhs, const pair<string, int> &rhs)
{
return lhs.second < rhs.second;
}

int main()
{
map<string, int> count;
string input;
getline(cin, input);
istringstream line(input);
string word;
while (line >> word)
count[word]++;
map<string, int>::iterator max = max_element(count.begin(), count.end(), compare);
cout << "max: " << max->first << ", " << max->second << " time(s)" << endl;
}
downmooner 2009-03-28
  • 打赏
  • 举报
回复
你太懒了。。。。只能说。在C++里vector其实就是比C的数组高级一点点的东东。。。
  • 打赏
  • 举报
回复
汗,这到题目给出的例子就是要连续输入啊.
不然例子怎么是 how, now now now now am am i am am am
而不是:
how,
now
now
......

而且你既然都说了把vector该成array就行,那你为什么不干脆改了呢.呵呵
不只是要写思路啊,还要安要求写出来嘛.
downmooner 2009-03-28
  • 打赏
  • 举报
回复

#include <sstream> //前面加个头文件。
...
...
int main()
{
vector<string> vec;
string sentence,word;
getline(cin,sentence);
stringstream ss(sentence);
while (ss>>word)
{
vec.push_back(word);
}
int res=findmax(vec);
cout<<vec[res]<<" "<<res<<endl;
system("pause");
return 0;
}
ltc_mouse 2009-03-28
  • 打赏
  • 举报
回复
只要记录之前连续输入最多次的字符串和上一次输入的字符串就可以吧
写一小段玩玩(以Ctrl+Z结束输入)

#include <iostream>
#include <string>

int main()
{
/* 在这里写程序 */
std::string result, last, input;
int max = 0, current = 0;
while(std::cin >> input)
{
if(last==input)
{
++current;
}
else
{
if( current > max )
{
max = current;
result = last;
}
last = input;
current = 1;
}
}
if( current > max )
{
max = current;
result = last;
}
std::cout<<std::endl<<"Count for string \""<<result<<"\" is "<<max<<std::endl;
return 0;
}
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 downmooner 的回复:]
你输入 估计是 一下输完

你输一个单词 敲下回车撒。。。笨啊。。我当然试了的
[/Quote]

汗,这到题目给出的例子就是要连续输入啊.
不然例子怎么是 how, now now now now am am i am am am
而不是:
how,
now
now
......
downmooner 2009-03-28
  • 打赏
  • 举报
回复
ww
ww
ww
qw1
qw1
qw1
qw1
asd
asd
^Z
qw1 3
请按任意键继续. . .
downmooner 2009-03-28
  • 打赏
  • 举报
回复
不想再输入 输入停止符 CTRL+Z
downmooner 2009-03-28
  • 打赏
  • 举报
回复
你输入 估计是 一下输完

你输一个单词 敲下回车撒。。。笨啊。。我当然试了的
  • 打赏
  • 举报
回复
你试着运行一下.不对哦.
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 downmooner 的回复:]
C/C++ code#include <iostream>
#include <string>
#include <vector>
using namespace std;
//输入是 how, now now now now am am i am am am
//则输出应表明"now"这个单词连续出现四次,是重复最多次的单词.

int findmax(vector<string> &vec)
{
int tempcount=1,bigcount=0,index=0;
string temp;
for (int i=1;i!=vec.size();++i)
{
temp=vec[i-1];
if (vec[i]==temp)

[/Quote]


不知道你有没有运行过哦.
这个程序运行后没次都是输出你输入的第一个单词,然后输出0.
不正确.
九桔猫 2009-03-28
  • 打赏
  • 举报
回复
我记得 C++Primer 上有这样一个实例
加载更多回复(4)

65,211

社区成员

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

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