zoukankan      html  css  js  c++  java
  • CodeForce 2A

    Winner

    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.

    Examples
    Input
    3
    mike 3
    andrew 5
    mike 2
    Output
    andrew
    Input
    3
    andrew 3
    andrew 2
    mike 5
    Output
    andrew

    题意:
      给出N轮游戏中的情况,求最后谁胜利了

      模拟水题,模拟----细节最重要
    AC代码:
    # include <iostream>
    # include <cstdio>
    # include <cstring>
    # include <algorithm>
    # include <map>
    using namespace std;
    
    const int MAXN = 1010;
    const int N =  50;
    
    int n , pos;
    int vis[MAXN];
    int p[MAXN];
    char start[MAXN][N];
    char name[MAXN][N];
    map<string , int> mp;
    
    int main()
    {
    	int max , cnt;
    	cin >> n;
    
    	for(int i = 0 ; i < n ; i++)
    		scanf("%s %d" , start[i] , &p[i]);
    
    	
    	max = 0 , pos = 0;
    	for(int i = 0 ; i < n ; i++)
    	{
    		int flag = -1;
    
    		for(int j = 0 ; j < pos ; j++)
    		{
    			if(!strcmp(name[j] , start[i]))
    			{
    				flag = j;
    				break;
    			}
    		}
    		if(flag == -1)
    			strcpy(name[pos++] , start[i]);
    	
    		mp[start[i]] += p[i];
    	} 
    
    	memset(vis , 0 , sizeof(vis));
    	for(int i = 0 ; i < pos ; i++)
    		max = max < mp[name[i]] ? mp[name[i]] : max;
    	for(int i = 0 ; i < pos ; i++)
    	{
    		if(max == mp[name[i]])
    		vis[i] = 1;
    	}
    
    	mp.clear();
    	for(int i = 0 ; i < n ; i++)
    	{
    		mp[start[i]] += p[i];
    		int tmp = 0;
    		for(int i = 0 ; i < pos ; i++)
    		{
    			if(tmp < mp[name[i]])
    			{
    			tmp = mp[name[i]];
    			cnt = i;
    		}
    	}
    
    		if(tmp >= max && vis[cnt])
    			break;
    	}
    
    	printf("%s
    " , name[cnt]);
       
       return 0;
    }
    
    
    
    生命不息,奋斗不止,这才叫青春,青春就是拥有热情相信未来。
  • 相关阅读:
    pdf文件的导入导出
    扩展方法用法整理
    c#批量插入数据库Demo
    Linq表达式和Lambda表达式用法对比
    Lambda表达式的诞生过程
    LeetCode77. Combinations(剑指offer38-2)
    LeetCode47.Permutations II(剑指offer38-1)
    LeetCode567. Permutation in String
    LeetCode46. Permutations
    图解HTTP-1.web和网络基础
  • 原文地址:https://www.cnblogs.com/lyf-acm/p/5787750.html
Copyright © 2011-2022 走看看