题解 | #记票统计#
记票统计
http://www.nowcoder.com/practice/3350d379a5d44054b219de7af6708894
思路简单,基本的字符串操作,值得注意的是数组是否包含某元素的判断,可使用Arrays.asList(arr).contains(s)判断。
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); LinkedHashMap<String,Integer> map = new LinkedHashMap<>(); String[] arr = new String[n]; int invalid=0; for(int i=0;i<n;i++){ String str = sc.next(); map.put(str,0); arr[i] = str; } int m = sc.nextInt(); for(int i=0;i<m;i++){ String s = sc.next(); if(Arrays.asList(arr).contains(s)){ map.put(s,map.get(s)+1); }else{ invalid++; } } for(String s : map.keySet()){ System.out.println(s+" : "+map.get(s)); } System.out.println("Invalid : "+invalid); } } }