题解 | #记票统计#
记票统计
https://www.nowcoder.com/practice/3350d379a5d44054b219de7af6708894
import java.util.*; public class Main { public static void main(String[] args) { Scanner fzhinput = new Scanner(System.in); LinkedHashMap<String,Integer> tptj = new LinkedHashMap<>(); int hxr = fzhinput.nextInt(); fzhinput.nextLine(); List<String> hxrmd = new ArrayList<>(); for(int i=0;i<hxr;i++){ hxrmd.add(fzhinput.next()); } hxrmd.add("Invalid"); int tpr = fzhinput.nextInt(); fzhinput.nextLine(); String tprmd[] = new String[tpr]; for(int i=0;i<tpr;i++){ tprmd[i]=fzhinput.next(); } for(int j=0;j<tpr;j++){ if(!hxrmd.contains(tprmd[j])){ tprmd[j]="Invalid"; } } for(int j=0;j<tpr;j++){ tptj.put(tprmd[j],tptj.getOrDefault(tprmd[j],0)+1); } for(int j=0;j<=hxr;j++){ tptj.put(hxrmd.get(j),tptj.getOrDefault(hxrmd.get(j),0)); } // List<Map.Entry<String,Integer>> result = new ArrayList<>(tptj.entrySet()); // Collections.sort(result, Comparator.comparing(Map.Entry<String,Integer>::getKey)); // for(int i=0;i<result.size();i++){ // Map.Entry<String,Integer>sc = result.get(i); // System.out.println(sc.getKey()+" : "+sc.getValue()); // } for(String z : hxrmd){ System.out.println(z+" : "+tptj.get(z)); } } }