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

关于C++的输入统计

关于C++的词汇统计
输入若干行,每行都有一条密码,最多100000行。输出:出现最多的3条密码,按照出现次数从大到小排序,若次数相同按照ASCII编码顺序。保证至少有3条不同密码。
输入样例:
123456
qwerty
12345678
123456
111111
1234567890
qwerty
123456
password
123123
987654321
输出样例:
123456
qwerty
111111


```c++
#include <bits/stdc++.h>
using namespace std;
const int N=100009;
struct pwd{
    string str;
    int c;
};
pwd f[N];
bool cmp(const pwd&a,const pwd&b){
    return a.c>b.c||a.c==b.c&&a.str<b.str;
}
int main()
{
    int n=0;
    map<string,int>::iterator mit;
    for(mit=d.begin();mit!=d.end();mit++)
    {
        f[n].str=mit->first;
        f[n].c=mit->second;
        n++;
    }
    sort(f,f+n,cmp);
    for(int i=0;i<3;i++)
    {
        cout<<f[i].str<<endl;
    }
    return 0;
}


```

  • 写回答

2条回答 默认 最新

  • CodeBytes 2023-02-02 19:40
    关注

    “该回答引用ChatGPT”
    代码如下

    #include <iostream>
    #include <map>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    struct Node {
        string password;
        int cnt;
    };
    
    bool cmp(Node a, Node b) {
        if (a.cnt != b.cnt) return a.cnt > b.cnt;
        return a.password < b.password;
    }
    
    int main() {
        map<string, int> m;
        string password;
        while (cin >> password) {
            m[password]++;
        }
        vector<Node> res;
        for (auto item : m) {
            res.push_back({item.first, item.second});
        }
        sort(res.begin(), res.end(), cmp);
        for (int i = 0; i < 3 && i < res.size(); i++) {
            cout << res[i].password << endl;
        }
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

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

悬赏问题

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