浙大ACM 1715 java 效率低吃内存 求高手指教改进
代码如下
import java.util.HashMap;
import java.util.Scanner;
import java.util.Map.Entry;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()){
String[] str = in.nextLine().split(" ");
int participants = Integer.parseInt(str[0]);
int quorum = Integer.parseInt(str[1]);
HashMap<String,Integer> occur=new HashMap<String,Integer>(10);
if(participants == 0 && quorum == 0)
break;
else{
for(int i = 0; i < participants; i++){
str = in.nextLine().split(" ");
for(int j = 1; j < str.length; j++)
occur.put(str[j], occur.containsKey(str[j]) ? occur.get(str[j])+1 : 1);
}
int someday = 100;
for(Entry<String, Integer> e : occur.entrySet()){
if(e.getValue() > quorum){
quorum = e.getValue();
someday = Integer.parseInt(e.getKey());
}
else if(e.getValue() == quorum && Integer.parseInt(e.getKey()) < someday){
someday = Integer.parseInt(e.getKey());
}
}
if(someday == 100)
someday = 0;
System.out.println(someday);
}
}
}
}