zoukankan      html  css  js  c++  java
  • [算法练习] UVA 10420

    UVA Online Judge 题目10420 - List of Conquests

    问题描述:

      题目很简单,给出一个出席宴会的人员列表,包括国籍和姓名(姓名完全没用)。统计每个国家有多少人参加,按国家名字典排序输出。

    输入格式:

      第一行是一个整数n,表示参加人数的个数。接下来是n行,每行第一个单词为国家名称,后面为姓名。

    输出格式:

      每行包括国家名称和出席人数,将国家名称按字典排序输出。

    示例输入:

    3
    Spain Donna Elvira
    England Jane Doe
    Spain Donna Anna

    示例输出:

    England 1
    Spain 2

    代码:(直接用STL吧。。太懒了)

     1 #include<iostream>
     2 #include<vector>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<string>
     6 #include<algorithm>
     7 using namespace std;
     8 
     9 struct Country{
    10     string name;
    11     int count = 0;
    12     bool operator == (const Country& b) {
    13         return this->name == b.name;
    14     }
    15 };
    16 
    17 int cmp(const Country& a, const Country& b){ return a.name < b.name; }
    18 
    19 vector<Country> Countries;
    20 int main()
    21 {
    22     int n;
    23     char str[75];
    24     scanf("%d", &n);
    25     for (int i = 0; i < n; i++)
    26     {
    27         scanf("%s", str);            //获得国家名
    28         char c;
    29         c = getchar();
    30         while (c != '
    '&&c != EOF){ c = getchar(); }    //忽视姓名
    31         Country nowCt;
    32         nowCt.name = str;
    33         vector<Country>::iterator iter = find(Countries.begin(), Countries.end(), nowCt);
    34         if (iter == Countries.end())
    35         {
    36             nowCt.count = 1;
    37             Countries.push_back(nowCt);
    38         }
    39         else
    40         {
    41             iter->count++;
    42         }
    43     }
    44     sort(Countries.begin(), Countries.end(), cmp);        //排序
    45     for (vector<Country>::iterator it = Countries.begin(); it != Countries.end();it++)
    46     {
    47         cout << it->name << " " << it->count << endl;
    48     }
    49     return 0;
    50 }
  • 相关阅读:
    docker 安装ELK
    关于centOS安装配置mysql5.6那点事
    关于centOS安装配置xampp那点事
    PowerDesigner16.5连接Oracle数据库生成E-R图
    Microsoft Visual Studio 中工具箱不显示DevExpress控件的解决办法
    Linux 基础命令
    oracle数据库表空间创建&导入&导出
    weblogic10.3.6漏洞修改方案
    oracle数据库表空间追加数据库文件方法
    转移博客
  • 原文地址:https://www.cnblogs.com/Leroy1245/p/3818688.html
Copyright © 2011-2022 走看看