• PAT——乙级1032


    这些题也确实简单,但是我还是想做做,多熟悉一下C++,毕竟实践是检验真理的唯一标准,有很多小知识点自己做了才知道。

    这个题是

    1032 挖掘机技术哪家强 (20 point(s))

    为了用事实说明挖掘机技术到底哪家强,PAT 组织了一场挖掘机技能大赛。现请你根据比赛结果统计出技术最强的那个学校。

    输入格式:

    输入在第 1 行给出不超过 105​​ 的正整数 N,即参赛人数。随后 N 行,每行给出一位参赛者的信息和成绩,包括其所代表的学校的编号(从 1 开始连续编号)、及其比赛成绩(百分制),中间以空格分隔。

    输出格式:

    在一行中给出总得分最高的学校的编号、及其总分,中间以空格分隔。题目保证答案唯一,没有并列。

    输入样例:

    6
    3 65
    2 80
    1 100
    2 70
    3 40
    3 0
    

    输出样例:

    2 150

    我是用的map做的,很简单。

     1 #include<map>
     2 #include<cstdio>
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     int n,school,score;
     8     map<int, int> score_Sum;
     9     scanf("%d", &n);
    10     while (n--)
    11     {
    12         scanf("%d%d", &school, &score);
    13         if (score_Sum.find(school) != score_Sum.end())//注意这里,如果没有找到,就返回的是最后一个数据的后面一个位置,也就是score_Sum.end()
    14         {
    15             score_Sum[school] += score;
    16         }
    17         else
    18         {
    19             score_Sum[school] = score;
    20         }
    21     }
    22     school = score = 0;
    23     map<int, int>::iterator it = score_Sum.begin();//遍历整个map的迭代器
    24     for (;it != score_Sum.end();it++)
    25     {
    26         if (it->second > score)
    27         {
    28             school = it->first;
    29             score = it->second;
    30         }
    31     }
    32     printf("%d %d", school, score);
    33 
    34     return 0;
    35 }

    这里面有一个小知识点

    就是map.find(key)。当它找不到这个key的时候,返回的是map.end(),也就是最后一个元素后面的一个位置。

    教材上是直接定义了一个大小为100010的数组,用的最暴力,最浪费空间的方法。暂时没什么借鉴的。

  • 相关阅读:
    UVA 11987 几乎就是并查集= =
    UVALive 5908 更新一下线段相交模板
    【poor几何】UVALive 5908 更新一下线段相交模板
    【poor几何】UVALive 5908 更新一下线段相交模板
    UVALive 3634 熟悉一下STL
    UVALive 3634 熟悉一下STL
    UVALive 3634 熟悉一下STL
    hdu2665 主席树模板题
    hdu2665 主席树模板题
    迷宫问题 POJ
  • 原文地址:https://www.cnblogs.com/albert-yzp/p/9956240.html
Copyright © 2020-2023  润新知