杭电 ACM 1004 java版 不知道为什么错了
importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.uti...
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = 0;
List<String> list = new ArrayList<String>();
N = in.nextInt();
while (N != 0) {
String name = null;
int max = 0;
boolean isX = true;
Map<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < N; i++) {
String key = in.next();
if (map.get(key) == null) {
Integer value = 1;
map.put(key, value);
if (isX) {
max = 1;
name = key;
isX = false;
}
} else {
Integer value = map.get(key) + 1;
map.remove(key);
map.put(key, value);
if (value > max) {
name = key;
}
}
}
list.add(name);
N = in.nextInt();
}
for (int j = 0; j < list.size(); j++) {
System.out.println(list.get(j));
}
System.out.println();
}
}
不知道那里错掉了 展开
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = 0;
List<String> list = new ArrayList<String>();
N = in.nextInt();
while (N != 0) {
String name = null;
int max = 0;
boolean isX = true;
Map<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < N; i++) {
String key = in.next();
if (map.get(key) == null) {
Integer value = 1;
map.put(key, value);
if (isX) {
max = 1;
name = key;
isX = false;
}
} else {
Integer value = map.get(key) + 1;
map.remove(key);
map.put(key, value);
if (value > max) {
name = key;
}
}
}
list.add(name);
N = in.nextInt();
}
for (int j = 0; j < list.size(); j++) {
System.out.println(list.get(j));
}
System.out.println();
}
}
不知道那里错掉了 展开
展开全部
没循环。
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
int T = scan.nextInt();
if (T == 0) {
break;
}
HashMap<String, Integer> hash = new HashMap<String, Integer>();
while ((T--) != 0) {
String str = scan.next();
int tmp;
if (!hash.containsKey(str)) {
tmp = 0;
} else {
tmp = hash.get(str).intValue() + 1;
}
hash.put(str, tmp);
}
Set<String> keyS = hash.keySet();
String ans = "";
int max = -1;
for (String str : keyS) {
int tmp = hash.get(str).intValue();
if (tmp > max) {
max = tmp;
ans = str;
}
}
System.out.println(ans);
}
}
}
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
int T = scan.nextInt();
if (T == 0) {
break;
}
HashMap<String, Integer> hash = new HashMap<String, Integer>();
while ((T--) != 0) {
String str = scan.next();
int tmp;
if (!hash.containsKey(str)) {
tmp = 0;
} else {
tmp = hash.get(str).intValue() + 1;
}
hash.put(str, tmp);
}
Set<String> keyS = hash.keySet();
String ans = "";
int max = -1;
for (String str : keyS) {
int tmp = hash.get(str).intValue();
if (tmp > max) {
max = tmp;
ans = str;
}
}
System.out.println(ans);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询