cf------(round 2)A. Winner
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
此题不大好懂,就是要你求出最大分数而且最先得到这个最高分的那个人的名字
思路: 第一步: 我们先求出所有人的最终得分
第二步: 我们求出最终得分的最高分:
第三部: 我们求出第一个得到最高分,而且最终得分也是最高分的那个人,那必定是最先出现的那个人.....
代码:
1 #include<iostream>
2 #include<map>
3 using namespace std;
4 struct node
5 {
6 int score;
7 string name;
8 }ss[1005];
9 int main(){
10
11 string winner;
12 int n,maxsc;
13 map<string ,int>str;
14 //freopen("test.in","r",stdin);
15 while(cin>>n){
16 if(!str.empty()) str.clear();
17 for(int i=0;i<n;i++){ //求出最终态势
18 cin>>ss[i].name>>ss[i].score;
19 str[ss[i].name]+=ss[i].score;
20 }
21 //求出最大的值
22 maxsc=str[ss[0].name];
23 for(int i=1;i<n;i++){
24 if(maxsc<str[ss[i].name]){
25 maxsc=str[ss[i].name];
26 }
27 }
28 map<string ,int>check; //这个表示过程态势
29 for(int i=0;i<n;i++){
30 check[ss[i].name]+=ss[i].score;
31 if(check[ss[i].name]>=maxsc&&str[ss[i].name]>=maxsc){
32 winner=ss[i].name;
33 break;
34 }
35 }
36 cout<<winner<<endl;
37 }
38 return 0;
39 }
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Elasticsearch: Reindex接口
- Android Studio 更新版本之后报错: Cannot access android.arch.lifecycle.ViewModelStoreOwner
- Android各版本查询和开启悬浮窗权限
- RuntimeException: setAudioSource failed异常
- Java&Android获取当前日期、时间、星期几、获取指定格式的日期时间、时间戳工具类包含使用示例
- Android TV 开发之 TV视频播放器
- Android MediaPlayer 音乐播放器扫描 本地音乐、上一曲、下一曲切歌、播放本地音乐
- 1小时真正掌握正则表达式
- Android Studio 安装配置教程 - Windows(详细版)
- Android 天气APP(八)城市切换 之 自定义弹窗与使用
- Android 接入udesk SDK实现IM
- Android 天气APP(十二)空气质量、UI优化调整
- Python+selenium 自动化-调用dom节点的click()监听事件,强力操作节点!穿透元素进行点击,节点被遮挡不可点击解决方法
- pkl是什么类型的文件?怎么来打开它?
- Python 技术篇-如何查看文本用什么类型的编码,文本编码查看方法