/* * Winner.cpp * * Created on: 2013-10-13 * Author: wangzhu */ /** * 先找出所有选手的分数和中最大的分数和,之后在所有选手的分数和中看有几个是和最大的分数和相等, * 1)、若有多个,则比较谁在比赛结束钱分数先达到最大分数和,则是赢家; * 2)、若只有一个,则直接输出。 */ #include<cstdio> #include<iostream> #include<map> #include<string.h> using namespace std; #define NMAX 1010 struct Node { int index, val; char name[33]; }; Node node[NMAX]; map<string, int> myMap; map<string, int> myMap1; map<string, int>::iterator mapIterator; void find(int n, int nmax) { int m; for (int i = 0; i < n; i++) { for (mapIterator = myMap1.begin(); mapIterator != myMap1.end(); mapIterator++) { if (0 == strcmp(node[i].name, mapIterator->first.c_str())) { // printf("%d %d %s ", node[i].index, node[i].val, node[i].name); m = myMap1[mapIterator->first]; m += node[i].val; myMap1[mapIterator->first] = m; if (m >= nmax) { printf("%s ", node[i].name); return; } } } } } int main() { freopen("data.in", "r", stdin); int n, m,nmax; string nmaxStr; while (~scanf("%d", &n)) { myMap.clear(); myMap1.clear(); for (int i = 0; i < n; i++) { scanf("%s%d", node[i].name, &node[i].val); node[i].index = i; myMap[node[i].name] +=node[i].val; } nmax = -1; for(mapIterator = myMap.begin();mapIterator!=myMap.end();mapIterator++){ if(nmax < mapIterator->second){ nmax = mapIterator->second; } } m = 0; for(mapIterator = myMap.begin();mapIterator!=myMap.end();mapIterator++){ if(nmax == mapIterator->second){ nmaxStr = mapIterator->first; myMap1[mapIterator->first] = 0; m ++; } } //printf("%d %d ",m,nmax); if(m != 1){ find(n,nmax); }else{ printf("%s ",nmaxStr.c_str()); } } return 0; }