Winner

这篇博客探讨了Berlogging卡片游戏的赢家判断算法。当游戏结束时,如果只有一个玩家拥有最大分数,则该玩家获胜。若多名玩家分数相同,获胜者为最先达到该最大分数的玩家。文章通过输入输出示例和代码解释了如何根据每个回合的得分更新并比较玩家状态,以确定最终赢家。
摘要由CSDN通过智能技术生成

序言
此篇博客作为自己的学习笔记,同时欢迎大家交流!

题目

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

代码
#include<iostream>
#include<cstring>
#include<algorithm>//习惯写上这三个头文件
#include<map>//map头文件

using namespace std;

map<string,int>ss,ss2;//声明两个map  一个记录最终得分,一个记录临时得分


int main()
{
	int n;
	cin>>n;
	string name[n];
    int  num[n],mx=1e-7;//因为得分有可能为负数,所以以无穷小为最大值,不会出现错误
    for(int i=0;i<n;i++)
    {
    	cin>>name[i]>>num[i];
    	ss[name[i]]+=num[i];	 
	}//第一个循环统计最终得分
	for(int i=0;i<n;i++) mx=max(mx,ss[name[i]]);//第二个循环统计最终最大值
	for(int i=0;i<n;i++)
	{
	   ss2[name[i]]+=num[i];
	   if(ss2[name[i]]>=mx&&ss[name[i]]>=mx){
	   //第三个循环统计首先到达最大值并且最终仍然是最大值的那个字符串
	   cout<<name[i]<<endl;//输出该字符串
	   return 0;//停止该程序
}
	}
	
}

可能有同学会有疑问,为什么不可以在循环的时候判断 比如:

 for(int i=0;i<n;i++)
    {
    	cin>>name[i]>>num[i];
    	ss[name[i]]+=num[i];
    	if(ss[name[i]]>=mx)
    		mx=ss[name[i]];	 

题目介绍中给出了,分数有可能为负数 (每次得到的分数在-1000到1000之间),所以有可能是在原来的基础上减去分数,比如最终的最大值是5,有可能这个人已经得到7分了,但最终减去了2分,为5,如果有这个循环判断的话,就会导致最大值是7,而不是5了,就会WA了;

(不要问我咋知道的,问就是我错过…)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值