程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Codeforces Beta Round #2

Codeforces Beta Round #2

編輯:C++入門知識

Codeforces Beta Round #2


 

A. Winner time limit per test 1 second memory limit per test 64 megabytes input standard input output standard output

The winner of the card game popular in Berland Berlogging is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line name score, where name is a player's name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to m) at the end of the game, than wins the one of them who scored at least m points first. Initially each player has 0 points. It's guaranteed that at the end of the game at least one player has a positive number of points.

Input

The first line contains an integer number n (1  ≤  n  ≤  1000), n is the number of rounds played. Then follow n lines, containing the information about the rounds in name score format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive.

Output

Print the name of the winner.

Sample test(s) Input
3
mike 3
andrew 5
mike 2
Output
andrew
Input
3
andrew 3
andrew 2
mike 5
Output
andrew

 

題意:給出一個各輪選手得分,最後輸出分最高的那個人.

如果有多人分數一樣高,那就輸出先達到這個分數的人.

題解:反正就是模擬了,直接用stl的map好了.

先遍歷每個人的得分 統計出最高的 在在這些人中遍歷 第一個達到最高得分的人輸出

第一次寫map 。。可能用的不是很好。

 

#include 
#include 
#include
using namespace std;

const int maxn = 1011;
char str[maxn][36];
int score[maxn];

int main(){
    int n;
    scanf(%d, &n);
    map name, nametmp;

    int nans = 0;
    for(int i = 0; i < n; ++i){
        scanf(%s%d, str[i], &score[i]);
        name[str[i]] += score[i];
    }

    for(int i = 0; i < n; ++i){
        if(name[str[i]] > nans){
            nans = name[str[i]];
        }
    }

    for(int i = 0; i < n; ++i){
        if(name[str[i]] == nans){
            nametmp[str[i]] += score[i];
            if(nametmp[str[i]] >= nans){
                puts(str[i]);
                break;
            }
        }
    }
    return 0;
}

 

 

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved