Loading... 给定一个由 0∼9 构成的长度为 N 的字符串,请你统计出现次数最多的数字。 ### 输入格式 输入包含多组测试数据。 每组数据占一行,包含一个字符串。 ### 输出格式 每组数据输出一行结果,表示出现最多的数字。 如果多个数字出现次数相同,则优先输出较小的数字。 ### 数据范围 \$1 \\leq N \\leq 1000\$ ### 输入样例 ``` 1234567891 11122333 1235564 ``` ### 输出样例 ``` 1 1 5 ``` ### 题目分析 红黑树 ``` #include <iostream> #include <map> using namespace std; char ch; string s; void solve() { map <char, int> mp; for (const char& ch : s) ++mp[ch]; int ans = 0; for (auto& t : mp) { if (ans < t.second) { ans = t.second; ch = t.first; } } cout << ch << '\n'; } int main () { ios::sync_with_stdio(0); cin.tie(0); while(cin >> s) solve(); return 0; } ``` 最后修改:2023 年 09 月 23 日 © 允许规范转载 赞 1 如果觉得我的文章对你有用,请随意赞赏