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 15686354699 at 2015-05-22 16:21:53 on Problem 1002
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class Main {   
  
    public static char getNum(char c) {   
        if (Character.isDigit(c)) {   
            return 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';   
        }   
        return '#';   
    }   
  
    public static void main(String[] args) throws IOException {   
    	Scanner in = new Scanner(System.in);
        Map< String, Integer> tm = new TreeMap<String, Integer>();   
        int n = in.nextInt();
        for (int i = 0; i < n; i++) {   
            String s = in.next().replace("-", "");   
            StringBuilder sb = new StringBuilder();   
            for (int k = 0; k < s.length(); k++) {   
                char c = getNum(s.charAt(k));  
                sb.append(c);
            }   
            String result = sb.insert(3, "-").toString();
            if (tm.containsKey(result)) {   
                int count = tm.get(result) + 1;   
                tm.put(result, count);   
            } else {   
                tm.put(result, 1);   
            }   
        }   
        Set se = tm.keySet();   
        Iterator it = se.iterator();   
        boolean flag = false;   
        while (it.hasNext()) {   
            String s = it.next().toString();   
            int count = tm.get(s);   
            if (count > 1) {   
                flag = true;   
                System.out.println(s + " " + count);   
            }   
        }   
        if (!flag) {   
            System.out.println("No duplicates. ");   
        }   
    }   
}

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