Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

JAVA测试结果是正确得,可是怎么也通不过,好烦。。。。。

Posted by 19990821 at 2020-01-21 15:58:36 on Problem 1002
public class Main {
    public static  void sort(String []arr,int k){
        boolean flag = true;
        String tmp;
        while(flag){
            flag = false;
            for(int i=0;i<k-1;i++)
            {
                if(arr[i].compareTo(arr[i+1])>0){
                    tmp = arr[i];
                    arr[i] = arr[i+1];
                    arr[i+1] = tmp;
                    flag = true;
                }
            }
        }
    }
    public static boolean isAlphabetic(char ch){
        return ch>='A'&&ch<='Z';
    }
    public static void main(String[] args){
        HashMap<Character, Integer> chars=new HashMap<Character, Integer>();
        chars.put('A',2);
        chars.put('B',2);
        chars.put('C',2);
        chars.put('D',3);
        chars.put('E',3);
        chars.put('F',3);
        chars.put('G',4);
        chars.put('H',4);
        chars.put('I',4);
        chars.put('J',5);
        chars.put('K',5);
        chars.put('L',5);
        chars.put('M',6);
        chars.put('N',6);
        chars.put('O',6);
        chars.put('P',7);
        chars.put('R',7);
        chars.put('S',7);
        chars.put('T',8);
        chars.put('U',8);
        chars.put('V',8);
        chars.put('W',9);
        chars.put('X',9);
        chars.put('Y',9);
        TreeMap<String,Integer> map =new TreeMap<String, Integer>();
        String temp[]=null;
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        String phonenum;
        char[] buf=null;
        StringBuilder ans;
        for (int i=0;i<n;i++){
            phonenum=sc.next();
            buf=phonenum.toCharArray();
            ans = new StringBuilder();
            for (char c : buf) {
                if (Character.isDigit(c)) {
                    ans.append(c);
                } else if (isAlphabetic(c)) {
                    ans.append(chars.get(c));
                }
            }
            if (map.containsKey(ans.toString())){
                map.put(ans.toString(),map.get(ans.toString())+1);
            }else{
                map.put(ans.toString(),1);
            }
        }
        temp=new String[map.size()];
        int k=0;
        for (Map.Entry<String,Integer> entry:map.entrySet())
        {
            if(entry.getValue()>=2){
                temp[k]=entry.getKey();
                k++;
            }
        }
        sort(temp,k);
        if (k==0)
            System.out.println("No duplicates");
        else
            for (int i=0;i<k;i++){
                for (int j=0;j<temp[i].length();j++){
                    if(j==3)
                        System.out.print("-");
                    System.out.print(temp[i].charAt(j));
                }
                System.out.println(" "+map.get(temp[i]));
            }
    }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator