• A


    Do you like playing basketball ? If you are , you may know the NBA Skills Challenge . It is the content of the basketball skills . It include several parts , such as passing , shooting , and so on. After completion of the content , the player who takes the shortest time will be the winner . Now give you their names and the time of finishing the competition , your task is to give out the rank of them ; please output their name and the rank, if they have the same time , the rank of them will be the same ,but you should output their names in lexicographic order.You may assume the names of the players are unique.

    Is it a very simple problem for you? Please accept it in ten minutes.

    InputThis problem contains multiple test cases! Ease test case contain a n(1<=n<=10) shows the number of players,then n lines will be given. Each line will contain the name of player and the time(mm:ss) of their finish.The end of the input will be indicated by an integer value of zero.
    OutputThe output format is shown as sample below.
    Please output the rank of all players, the output format is shown as sample below;
    Output a blank line between two cases.Sample Input

    10
    Iverson 17:19
    Bryant 07:03
    Nash 09:33
    Wade 07:03
    Davies 11:13
    Carter 14:28
    Jordan 29:34
    James 20:48
    Parker 24:49
    Kidd 26:46
    0

    Sample Output

    Case #1
    Bryant 1
    Wade 1
    Nash 3
    Davies 4
    Carter 5
    Iverson 6
    James 7
    Parkr 8
    Kidd 9
    Jordan 10


    主要:设计思路,利用结构体和时间的的两个单位都化为秒。
       这道题主要考察,输出格式(每个case之间有空行)相同名次的输出,和sort排列的使用。
    代码如下:

    #include<iostream>
    #include<string>
    #include<algorithm>
    using namespace std;
    struct H
    {
     string name;
     int num;
    };
    H h[15];
    bool cmp(H a, H b)
    {
     if (a.num == b.num) return a.name < b.name;
     return a.num<b.num;
    }
    int main()
    {
     int num,time,t=1,ans=0;
     while (cin >> num)
     {
      if (num == 0) break;
      for (int i = 0; i < num; i++)
      {
       cin >> h[i].name;
       cin >> time;
       time *= 60;
       h[i].num = time;
       getchar();
       cin >> time;
       h[i].num += time;
      }
      sort(h, h + num, cmp);

      if (ans) cout << endl;
      cout << "Case #" << t++ << endl;
      int k ,p=0;
      k = 1;
      for (int i = 0; i < num; i++)
      {
       if (h[i].num == h[i - 1].num&&i!=0)
       {
        p++;
        cout << h[i].name << " " << k-p << endl;
       }
       else
       {
        p = 0;
        cout << h[i].name << " " << k << endl;
       }
       k++;
      }
      ans = 1;
     }
     return 0;
    }

    注意:每个人都有不同的方式表达逻辑方法和习惯,比如我比较喜欢手画出来,我们多用更熟悉的方式来ac。

        作者在做这道题的时候,想象名次的输出的时候wa了多次,但是,画一画马上就有很清晰的逻辑关系了。

        也算是自己的一点儿小经验吧。

  • 相关阅读:
    [LOJ#6284.数列分块入门8] ODT(珂朵莉树)解法
    [CF Contest] Sum of Round Numbers 题解
    基础数论记录
    [CF Contest] Kana and Dragon Quest game 题解
    hexo上怎么写博客
    keepalived的部署
    python局部和全局变量
    python发送邮件
    lamp架构的部署
    rayns数据同步
  • 原文地址:https://www.cnblogs.com/damaoranran/p/8778225.html
Copyright © 2020-2023  润新知