您的位置:首页 > 编程语言 > Java开发

acm 1002

2014-04-11 20:56 357 查看
package com.tset;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Iterator;

import java.util.Map;

import java.util.TreeMap;

/**

 *

 *@author dsw

 *A, B, and C map to 2

  D, E, and F map to 3

  G, H, and I map to 4

  J, K, and L map to 5

  M, N, and O map to 6

  P, R, and S map to 7

  T, U, and V map to 8

  W, X, and Y map to 9

 *input

 * 12

   4873279

   ITS-EASY

   888-4567

   3-10-10-10

   888-GLOP

   TUT-GLOP

   967-11-11

   310-GINO

   F101010

   888-1200

   -4-8-7-3-2-7-9-

   487-3279

 *

 *output

 *310-1010 2

  487-3279 4

  888-4567 3

 */

public class Test_1002 {

    public static void main(String args[]){

        Test_1002 t = new Test_1002();

        //Scanner sc = new Scanner(System.in);

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int tot;

        try {

            tot = Integer.parseInt(br.readLine());

            Map<String, Integer> map = new TreeMap<String, Integer>();

            for(int i=0;i<tot;i++){

                String numstr = br.readLine().trim();

                numstr = t.setNumber(numstr);

                if(numstr.length()==8){

                    //判断是否已经存在,存在加1,不存在变为1.

                    if(map.containsKey(numstr)){

                        map.put(numstr,Integer.parseInt(map.get(numstr).toString())+1 );

                    } else {

                        map.put(numstr, 1);

                    }

                }

            }

            //迭代判断把出现次数大于1的输出

            Iterator it = map.keySet().iterator();

            while(it.hasNext()){

                String key = it.next().toString();

                int value = Integer.parseInt(map.get(key).toString());

                if(value!=1){

                    System.out.println(key+" "+value);

                } else ;

            }

        } catch (NumberFormatException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

    //将输入的电话号码转化为标准的格式

    public String setNumber(String s){

        s = s.replace("-","");

        StringBuffer sb = new StringBuffer(s);

        for(int i=0;i<sb.length();i++){

            switch(sb.charAt(i)){

            case 'A':

            case 'B':

            case 'C':

                sb.setCharAt(i, (char)50);//ASCII=50的字符:2

                break;

            case 'D':

            case 'E':

            case 'F':

                sb.setCharAt(i, (char)51);

                break;

            case 'G':

            case 'H':

            case 'I':

                sb.setCharAt(i, (char)52);

                break;

            case 'J':

            case 'K':

            case 'L':

                sb.setCharAt(i, (char)53);

                break;

            case 'M':

            case 'N':

            case 'O':

                sb.setCharAt(i, (char)54);

                break;

            case 'P':

            case 'R':

            case 'S':

                sb.setCharAt(i, (char)55);

                break;

            case 'T':

            case 'U':

            case 'V':

                sb.setCharAt(i, (char)56);

                break;

            case 'W':

            case 'X':

            case 'Y':

                sb.setCharAt(i, (char)57);

                break;

            default:

                ;

            }

        }

        return sb.insert(3, "-").toString();

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 算法 acm