输入输出怎么写,希望大家帮忙补充。
C++词汇统计
把所有单词/短语汇总在一起,找出出现次数最多的那个。
输入是一行,为所有单词,由空格隔开。最多有100000个单词。
输出一个出现次数最多的单词,如有相同次数,输出字典序靠前的。
输入样例:
dangran nage nage haode dangran qiche nage youdaoli youyisi nage nihao henhao henxianran verygood
输出样例:
nage
#include <bits/stdc++.h>
using namespace std;
int main()
{
int big=0;
map<string,int>::iterator mit;
for(mit=d.begin();mit!=d.end();mit++)
{
int cnt=mit->second;
if(cnt>big)
{
big=cnt
ans=mit->first;
}
}
return 0;
}
“该回答引用ChatGPT”
请参考下面代码,谢谢:
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
int main() {
map<string, int> words;
string word;
while (cin >> word) {
++words[word];
}
string res;
int max_count = 0;
for (auto item : words) {
if (item.second > max_count) {
max_count = item.second;
res = item.first;
} else if (item.second == max_count) {
if (item.first < res) res = item.first;
}
}
cout << res << endl;
return 0;
}
是以回车作为输入结束吗