第一行输入一个整数
代表候选人数。
第二行输入
个长度为
、仅由大写字母构成的字符串
,代表候选人的名字。保证候选人的名字互不相同。
第三行输入一个整数
代表投票人数。
第四行输入
个长度为
、仅由大写字母构成的字符串
,代表投票内容。
对于每一位候选人,新起一行。先输出其名字,随后输出一个空格、一个冒号、一个空格作为间隔,最后输出其获得的票数。形如
,其中
是候选人的名字,
是候选人的票数。
最后一行以相同的格式输出无效票的数量。形如
,其中
是无效票的数量。
4 A B C D 8 A D E CF A GG A B
A : 3 B : 1 C : 0 D : 1 Invalid : 3
在这个样例中,
三张票是无效的。
import java.util.Scanner; import java.util.LinkedHashMap; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); LinkedHashMap<String, Integer> h = new LinkedHashMap<>(); for (int i = 0; i < n; i++) { h.put(in.next(), 0); } int p = in.nextInt(); for (int i = 0; i < p; i++) { String name = in.next(); if (h.containsKey(name)) { h.put(name, h.get(name)+1); } } int count = 0; for (String k : h.keySet()) { System.out.println(k + " : " + h.get(k)); count += h.get(k); } System.out.println("Invalid : " + (p - count)); } }
import java.util.Scanner; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.Map; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); LinkedHashMap<String, Integer> hm = new LinkedHashMap(); for (int i = 0; i < num; i++) { hm.put(sc.next(), 0); } int num2 = sc.nextInt(); int InvalidNum=0; for (int i = 0; i < num2; i++) { String name = sc.next(); if(hm.containsKey(name)){ int count=hm.get(name); hm.put(name,++count); }else{ InvalidNum++; } } for(Map.Entry<String,Integer> entry:hm.entrySet()){ System.out.println(entry.getKey()+" : "+entry.getValue()); } System.out.println("Invalid : "+InvalidNum); } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); List<String> list = new ArrayList(); int n = in.nextInt(); for(int i = 0; i < n; i++){ list.add(in.next()); } int m = in.nextInt(); int err = 0; Map<String, Integer> map = new HashMap(); for(int i = 0; i < m; i++){ String s = in.next(); if(list.contains(s)){ map.put(s, map.getOrDefault(s, 0)+1); }else{ err++; } } for(String name : list){ System.out.println(name+" : "+ map.getOrDefault(name,0)); } System.out.println("Invalid : "+err); } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int a = in.nextInt(); List<String> listA = new ArrayList<>(); List<String> listB = new ArrayList<>(); while (a -- > 0) { listA.add(in.next()); } int b = in.nextInt(); while (b-- > 0) { listB.add(in.next()); } Map<String, Integer> map = new HashMap<>(); int invalid = 0; for (String list : listA) { map.put(list, 0); } for (String list : listB) { if (map.containsKey(list)) { map.put(list, map.getOrDefault(list, 0) + 1); } else { invalid++; } } listA.forEach(li -> { System.out.println(li + " : " + map.get(li)); }); System.out.println("Invalid : " + invalid); } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); LinkedHashMap<String, Integer> map = new LinkedHashMap<>(); int n1 = in.nextInt(); // 初始化计数器 for (int i = 0; i < n1; i++) { map.putIfAbsent(in.next(), 0); } map.put("Invalid", 0); // 计数 int n2 = in.nextInt(); for (int i = 0; i < n2; i++) { String d = in.next(); if (map.containsKey(d)) { map.compute(d, (k, v) -> v + 1); } else { map.compute("Invalid", (k, v) -> v + 1); } } // 输出 Set<Map.Entry<String, Integer>> set = map.entrySet(); for (Map.Entry entry : set ) { System.out.println(String.format("%s : %d", entry.getKey(), entry.getValue())); } } }
import java.util.HashMap; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case int candidateNum = in.nextInt(); HashMap<String,Integer> voteCount = new HashMap<>(); for(int i=0;i<candidateNum;i++){ voteCount.put(in.next(),0); } int voteNum = in.nextInt(); for(int i=0;i<voteNum;i++){ String key =in.next(); Integer num = voteCount.get(key); if(num==null){ voteCount.put("Invalid", voteCount.getOrDefault("Invalid",0)+1); }else{ voteCount.put(key,num+1); } } voteCount.keySet().forEach(key->{ System.out.println(key+" : "+voteCount.get(key)); }); } } }
import java.util.Arrays; import java.util.LinkedHashMap; import java.util.Map; import java.util.Scanner; import java.util.stream.Collectors; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case in.nextInt(); in.nextLine(); Map<String, Integer> map = Arrays.stream(in.nextLine().split(" ")).collect(Collectors.toMap(s -> s, s -> 0, (oldValue, newValue) -> 0, LinkedHashMap::new)); map.put("Invalid", 0); int m = in.nextInt(); while (m-- > 0) { Integer value = map.computeIfPresent(in.next(), (s, integer) -> integer + 1); map.compute("Invalid", (s, integer) -> integer + (value == null ? 1 : 0)); } map.forEach((key, value) -> System.out.println(key + " : " + value)); } } }
import java.util.Scanner; import java.util.Objects; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case int a = Integer.parseInt(in.nextLine()); String[][] arr1 = new String[a][2]; String str1 = in.nextLine(); String[] arr2 = str1.split(" "); for (int i = 0; i < a; i++) { arr1[i][0] = arr2[i]; } int b = Integer.parseInt(in.nextLine()); String str2 = in.nextLine(); String[] arr3 = str2.split(" "); int invalid = b; for (int i = 0; i < a; i++) { int num = 0; for (int j = 0; j < b; j++) { if (Objects.equals(arr1[i][0], arr3[j])) { num++; invalid--; } } arr1[i][1] = String.valueOf(num); } for (int i = 0; i < a; i++) { System.out.println(arr1[i][0] + " : " + arr1[i][1]); } System.out.println("Invalid : " + invalid); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedHashMap; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int houXuanRenNumber = Integer.parseInt(br.readLine()); String[] houXuanRens = br.readLine().split(" "); int touPiaoRenNumber = Integer.parseInt(br.readLine()); String[] resultTouPiao = br.readLine().split(" "); // 保证插入顺序和取出顺序一致,使用LinkedHashMap集合 LinkedHashMap<String, Integer> hashMap = new LinkedHashMap<>(); int invalidNum = 0; for(int i = 0; i < houXuanRens.length; i++){ hashMap.put(houXuanRens[i], 0); } hashMap.put("Invalid", 0); for(int i = 0; i < resultTouPiao.length; i++){ String temp = resultTouPiao[i]; if(hashMap.containsKey(temp)){ hashMap.put(temp, hashMap.get(temp) + 1); }else{ hashMap.put("Invalid", hashMap.get("Invalid") + 1); } } for(String key : hashMap.keySet()){ System.out.println(key + " : " + hashMap.get(key)); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; while((str=br.readLine())!=null){ int cou=Integer.parseInt(str); int[] dd=new int[cou]; String[] strArr=br.readLine().split(" "); int num=Integer.parseInt(br.readLine()); String[] strArr1=br.readLine().split(" "); int inv=0; for(int i=0;i<strArr1.length;i++){ boolean b=false; for(int j=0;j<strArr.length;j++){ if(strArr1[i].equals(strArr[j])){ dd[j]+=1; b=true; break; } } if(!b){ inv++; } } for(int i=0;i<cou;i++){ System.out.println(strArr[i]+" "+":"+" "+dd[i]); } System.out.println("Invalid : "+inv); } } }
import java.util.*; import javax.script.*; public class Main { public static void main(String[] args){ Scanner scan = new Scanner(System.in); while(scan.hasNext()) { Map<String,Integer> map=new LinkedHashMap(); //非法参数 int invalid=0; int n=Integer.parseInt(scan.nextLine()); String s=scan.nextLine(); //用空格分割开 String str[]=s.split("\\s+"); for(int i=0;i<str.length;i++) { map.put(str[i],0); } int m=Integer.parseInt(scan.nextLine()); String str2[]=scan.nextLine().split("\\s+"); for(int i=0;i<str2.length;i++) { if(map.containsKey(str2[i])) { map.put(str2[i],map.get(str2[i])+1); }else { invalid++; } } for(Map.Entry<String,Integer > entry:map.entrySet()){ System.out.print(entry.getKey()+" : "+entry.getValue()+"\n"); } System.out.println("Invalid : "+invalid); } } }
/*** 12/26 运行时间:44ms;占用内存:10980KB ***/ import java.util.Scanner; import java.util.HashMap; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case int cand = Integer.valueOf(in.nextLine()); //候选人的人数 String[] names = in.nextLine().split(" "); //n个候选人的名字 int voters = Integer.valueOf(in.nextLine()); //投票人的人数 String[] votes = in.nextLine().split(" "); //投票 int Invalid = 0; //一个一个判断对比太麻烦了,转用HashMap,只存储合法的名字和对应的票数 HashMap<String,Integer> map = new HashMap<>(); for(String s : names){ map.put(s,0); } for(String s : votes){ if(map.containsKey(s)){ map.put(s, map.getOrDefault(s,0)+1); }else{ Invalid++; } } for(String s : names){ System.out.println(s + " : " + map.get(s)); } System.out.println("Invalid : "+Invalid); } } }