杭电ACM1004题,let the balloon rise java,测试用例能通过,提交总是wrong answer,求指点(Java版)。
importjava.util.HashMap;importjava.util.Map.Entry;importjava.util.Scanner;classBalloo...
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;
class Balloon {
public static void main(String[] args) {
HashMap<String, Integer> hm=new HashMap<String, Integer>();;
Scanner input = new Scanner(System.in);
int number = 0, max = 0, i = 0;
String key=null;
String result = null;
while ((number = input.nextInt())!=0) {
hm.clear();
for (i = 0; i < number; i++) {
key = input.next();
if (!hm.containsKey(key))
hm.put(key, 1);
else {
hm.put(key, hm.get(key) + 1);
}
}
max = 0;
for (Entry<String, Integer> entry : hm.entrySet()) {
if (entry.getValue() > max) {
result = entry.getKey();
if (result != null)
max = entry.getValue();
}
}
System.out.println();
System.out.print(result);
}
input.close();
}
} 展开
import java.util.Map.Entry;
import java.util.Scanner;
class Balloon {
public static void main(String[] args) {
HashMap<String, Integer> hm=new HashMap<String, Integer>();;
Scanner input = new Scanner(System.in);
int number = 0, max = 0, i = 0;
String key=null;
String result = null;
while ((number = input.nextInt())!=0) {
hm.clear();
for (i = 0; i < number; i++) {
key = input.next();
if (!hm.containsKey(key))
hm.put(key, 1);
else {
hm.put(key, hm.get(key) + 1);
}
}
max = 0;
for (Entry<String, Integer> entry : hm.entrySet()) {
if (entry.getValue() > max) {
result = entry.getKey();
if (result != null)
max = entry.getValue();
}
}
System.out.println();
System.out.print(result);
}
input.close();
}
} 展开
1个回答
展开全部
你好,杭电上java代码有要求的。
java代码的类必须叫Main,而你的是Ballon。改过就好了。
还有这题输出的时候System.out.println(result);就好了,不能分开。
改正后AC代码:
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
HashMap<String, Integer> hm=new HashMap<String, Integer>();;
Scanner input = new Scanner(System.in);
int number = 0, max = 0, i = 0;
String key=null;
String result = null;
while ((number = input.nextInt())!=0) {
hm.clear();
for (i = 0; i < number; i++) {
key = input.next();
if (!hm.containsKey(key))
hm.put(key, 1);
else {
hm.put(key, hm.get(key) + 1);
}
}
max = 0;
for (Entry<String, Integer> entry : hm.entrySet()) {
if (entry.getValue() > max) {
result = entry.getKey();
if (result != null)
max = entry.getValue();
}
}
System.out.println(result);
}
input.close();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询