题目没截下图来,大致使这样的:
多个人玩游戏,没人输入为多组,每次中有多行输入,第一行输入该组中有几条数据,然后下面每一行包括一个名字和一个分数,若分数为负,这表示该玩家扣分,比赛结果为该轮中,分数最高者胜利,如果有多人获得最高分,则约定谁先获得最高分,谁就胜利。
给出的样例大致如下:
输入
3
a 3
b 5
a 2
2
a 3
a 2
b 5
输出:
b
a
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class 卡牌游戏 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int N = sc.nextInt();
for (int i = 0; i < N; i++) {
int n = sc.nextInt();
String name;
int score;
Map<String,Integer> m = new HashMap<String, Integer>();
int max = 0;
String win = "";
for (int j = 0; j < n; j++) {
name = sc.next();
score = sc.nextInt();
boolean is = false;
//判断是否存在name
for (String n1 : m.keySet()) {
if(n1.equals(name)) is = true;
}
if(is){
score = score + m.get(name);
if(score > max){
max = score;
win = name;
}
m.put(name, score);
}else{
if(score > max){
max = score;
win = name;
}
m.put(name, score);
}
}
System.out.println(win);
}
}
}
}
这样写只过了60%,不知道还有哪里没有考虑到,请各位朋友指点。