首页 > 代码库 > 算法:号码转换问题

算法:号码转换问题

 

该题太水,没必要参考。

 

package practice;import java.io.BufferedInputStream;import java.util.Map;import java.util.Scanner;import java.util.TreeMap;/** * @author caiyu * @date 2014-11-2 */public class MemorableTelNumber {    public static void main(String[] args) {        Scanner cin = new Scanner(new BufferedInputStream(System.in));        int l = cin.nextInt();        String num;        Map<String, Integer> m = new TreeMap<String, Integer>();        StringBuffer c;        int tmp;        String s;        for (int i = 0; i < l; i++) {            num = cin.next().trim();            c = new StringBuffer();            for (int j = 0, len = num.length(); j < len; j++) {                tmp = handle(num.charAt(j));                if (0 <= tmp && tmp <= 9)                    c.append(tmp);            }            if (c.length() == 7) {                c.insert(3, "-");                s = c.toString();                if (m.containsKey(s))                    m.put(s, m.get(s) + 1);                else                    m.put(s, 1);            }        }        for (String k : m.keySet()) {            if (m.get(k) > 1)                System.out.println(k + " " + m.get(k));        }    }    private static int handle(char c) {        if (c == ‘A‘ || c == ‘B‘ || c == ‘C‘)            return 2;        if (c == ‘D‘ || c == ‘E‘ || c == ‘F‘)            return 3;        if (c == ‘G‘ || c == ‘H‘ || c == ‘I‘)            return 4;        if (c == ‘J‘ || c == ‘K‘ || c == ‘L‘)            return 5;        if (c == ‘M‘ || c == ‘N‘ || c == ‘O‘)            return 6;        if (c == ‘P‘ || c == ‘R‘ || c == ‘S‘)            return 7;        if (c == ‘T‘ || c == ‘U‘ || c == ‘V‘)            return 8;        if (c == ‘W‘ || c == ‘X‘ || c == ‘Y‘)            return 9;        if (c == ‘-‘)            return -1;        return Integer.parseInt("" + c);    }}
View Code

 

算法:号码转换问题