每天一道算法_487-3279_对电话号码格式化统计批处理
早上弄了一道求高精度幂的算法,偷懒用了内部类,总觉得过意不去,所以今天重新做了一道算法题,做完心里舒服好多。
题目如下:
import java.util.ArrayList;import java.util.Scanner;public class ThePhoneNum {public static void main(String args[]) {String[] tem = new String[8];//用来存储格式化之后的电话号码tem[3] = "-";//把电话号码第四位固定为'-'ArrayList<String> list = new ArrayList<String>();//记录输入的不规范号码ArrayList<String> memory = new ArrayList<String>();//用来记录格式化之后的所有号码Scanner scanner = new Scanner(System.in);int num = scanner.nextInt();int[] count = new int[num]; //统计相同号码的数量while ((num--) >= 0) {list.add(scanner.nextLine());}for (int i = 0; i < list.size(); i++) {String string = list.get(i);String[] temparray = string.split("-");//除去输入的号码中的所有"-"string = getStr(temparray);//把上行代码得到的字符串数组转化成字符串for (int j = 0; j < string.length(); j++) {String temString = string.substring(j, j + 1);if (j < 3) {if (isLetter(temString)) {//是否为字母tem[j] = toNum(temString);//把字母映射成数字} else {tem[j] = string.substring(j, j + 1);//数字不用映射}} else {if (isLetter(temString)) {tem[j + 1] = toNum(temString);} else {tem[j + 1] = string.substring(j, j + 1);}}}if (tem[1] != null) {String s = getStr(tem);//把经过转换后的号码加入到列表存储起来if(memory.contains(s)){count[memory.indexOf(s)]++;//如果已经存在,则直接数量上加1}else{memory.add(s);}}}for(String s : memory){System.out.println(s + " "+ (count[memory.indexOf(s)]+1));//遍历列表,输出号码}}private static String toNum(String temString) {// TODO Auto-generated method stubchar c = temString.charAt(0);String s = String.valueOf(c);switch (c) {case 'a':case 'b':case 'c':case 'A':case 'B':case 'C':s = "2";break;case 'd':case 'e':case 'f':case 'D':case 'E':case 'F':s = "3";break;case 'g':case 'h':case 'i':case 'G':case 'H':case 'I':s = "4";break;case 'j':case 'k':case 'l':case 'J':case 'K':case 'L':s = "5";break;case 'm':case 'n':case 'o':case 'M':case 'N':case 'O':s = "6";break;case 'p':case 'r':case 's':case 'P':case 'R':case 'S':s = "7";break;case 'T':case 'U':case 'V':case 't':case 'u':case 'v':s = "8";break;case 'w':case 'x':case 'y':case 'W':case 'X':case 'Y':s = "9";break;}return s;}static boolean isLetter(String s) {char c = s.charAt(0);if (c >= 65 && c <= 122) {return true;} else {return false;}}public static String getStr(String[] args){String str="";for(int i=0;i<args.length;i++){str+=(String)args[i];}return str;}}
作者:jason0539
微博:http://weibo.com/2553717707
博客:http://blog.csdn.net/jason0539(转载请说明出处)