_纵拥千千晚星 2023-02-03 10:52 采纳率: 100%
浏览 34
已结题

关于C++英文统计程序

关于C++英文统计
英文里出现最频繁的字母是小写e。你希望统计一下,在一段英文文章里出现最频繁的双字母组合和三字母组合是什么?
输入样例#1:
Talk is cheap. Show me the code. Good design adds value faster than it adds cost. In theory, theory and practice are the same. In practice, they're not. Debugging is twice as hard as writing the code in the first place.
输出样例#1:
he
the


```c++
#include <bits/stdc++.h>
using namespace std;
map<string,int>d;
int main()
{
    string s,ans;
    int big=0;
    while(cin>>s)
    {
        d[s]++;
    }
    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;
        }
        else if(cnt==big)
        {
            if (mit->first<ans)
            {
                ans=mit->first;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

```

  • 写回答

2条回答 默认 最新

  • CodeBytes 2023-02-03 14:51
    关注

    “该回答引用ChatGPT”
    参考下面的代码

    #include <iostream>
    #include <map>
    #include <string>
    using namespace std;
    
    int main() {
        string s;
        getline(cin, s);
    
        // 统计双字母组合的频率
        map<string, int> double_letters;
        for (int i = 0; i < s.size() - 1; i++) {
            string double_letter = s.substr(i, 2);
            double_letters[double_letter]++;
        }
    
        // 统计三字母组合的频率
        map<string, int> triple_letters;
        for (int i = 0; i < s.size() - 2; i++) {
            string triple_letter = s.substr(i, 3);
            triple_letters[triple_letter]++;
        }
    
        // 查找最频繁的双字母组合
        string max_double_letter = "";
        int max_double_letter_count = 0;
        for (auto it = double_letters.begin(); it != double_letters.end(); it++) {
            if (it->second > max_double_letter_count) {
                max_double_letter = it->first;
                max_double_letter_count = it->second;
            }
        }
    
        // 查找最频繁的三字母组合
        string max_triple_letter = "";
        int max_triple_letter_count = 0;
        for (auto it = triple_letters.begin(); it != triple_letters.end(); it++) {
            if (it->second > max_triple_letter_count) {
                max_triple_letter = it->first;
                max_triple_letter_count = it->second;
            }
        }
    
        cout << max_double_letter << endl << max_triple_letter << endl;
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月6日
  • 已采纳回答 2月3日
  • 修改了问题 2月3日
  • 创建了问题 2月3日

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址