题意:就是输入几组字符串,按照给出的形式转换为统一格式,然后输出出现多于一次的字符串出现的次数,输出时按照升序排列。
这道题也很简单的,我是用Map做的,感觉Map在有的方便的运用会使问题变得很简单……
下面直接贴出我的AC代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import java.util.Arrays; import java.util.HashMap; import java.util.Scanner; public class Main{ public static void main(String[] args) { HashMap<String, Integer> hm = new HashMap<String, Integer>(); Scanner in = new Scanner(System.in); int col = in.nextInt(); for(int i = 0; i < col; i++){ //每读一次的处理 StringBuffer sb = new StringBuffer(); String next = in.next(); int len = next.length(); for(int j = 0; j < len; j++){ char nc = next.charAt(j); if(nc!='-'){ int gm = 0; if(nc>='0'&&nc<='9')gm = (nc-48); else gm = getNum(nc); sb.append(gm); } } String key = sb.toString(); if(hm.containsKey(key)){ hm.put(key, hm.get(key)+1); }else{ hm.put(key, 1); } sb.setLength(0); } Object[] keys = hm.keySet().toArray(); Arrays.sort(keys); boolean b = false; for(Object k : keys){ String kk = (String)k; if(hm.get(kk)>1){ b = true; System.out.println(kk.substring(0,3) + "-" + kk.substring(3) + " "+hm.get(kk)); } } if(!b){ System.out.println("No duplicates."); } } private static int getNum(char c){ if(c=='A'||c=='B'||c=='C')return 2; else if(c=='D'||c=='E'||c=='F')return 3; else if(c=='G'||c=='H'||c=='I')return 4; else if(c=='J'||c=='K'||c=='L')return 5; else if(c=='M'||c=='N'||c=='O')return 6; else if(c=='P'||c=='R'||c=='S')return 7; else if(c=='T'||c=='U'||c=='V')return 8; else return 9; } } |
除非注明,饮水思源博客文章均为原创,转载请以链接形式标明本文地址