题解 | 记票统计

#include <iostream>
#include <map>
#include <vector>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<string> names;
    string temp;
    for (int i = 0; i < n; i ++) {
        cin >> temp;
        names.push_back(temp);
    }

    int m;
    cin >> m;
    map<string, int> map_tickets;
    // map 赋初值 即每个候选人的票数为0
    for (string name : names) {
        map_tickets[name] = 0;
    }

    // 无效票标识
    int invalid_flag = 1;
    for (int i = 0; i < m; i ++) {
        cin >> temp;
        invalid_flag = 1;
        // 只要投票名在候选人中,就退出
        for (string name : names) {
            if (temp == name) {
                invalid_flag = 0;
                break;
            }
        }

        if (invalid_flag == 0) {
            map_tickets[temp] ++;
        } else {
            map_tickets["Invalid"] ++;
        }
    }

    // 打印结果
    for (string name : names) {
        cout << name << " : " << map_tickets[name] << endl;
    }
    cout << "Invalid : " << map_tickets["Invalid"] << endl;

}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务