浙大ACM 1715 java 效率低吃内存 求高手指教改进

aneallu 2012-08-27 05:07:33
代码如下
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);
}
}
}
}
...全文
57 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧