Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:第一次弄这个练习In Reply To:Re:第一次弄这个练习 Posted by:fishing_test at 2009-04-16 20:46:29 > 我写的, 可读性好点, 呵呵 > > import java.io.BufferedReader; > import java.io.InputStreamReader; > import java.util.ArrayList; > import java.util.Collections; > import java.util.Comparator; > import java.util.HashMap; > import java.util.List; > import java.util.Map; > > public class P1002 { > > private static String convert(String line) { > String ret = ""; > > for (int i = 0; i < line.length(); i++) { > if (line.charAt(i) >= '0' && line.charAt(i) <= '9') { > ret += line.charAt(i); > } else if (line.charAt(i) >= 'A' && line.charAt(i) <= 'Z') { > > char c = line.charAt(i); > > if (c == 'A' || c == 'B' || c == 'C') { > ret += "2"; > } else if (c == 'D' || c == 'E' || c == 'F') { > ret += "3"; > } else if (c == 'G' || c == 'H' || c == 'I') { > ret += "4"; > } else if (c == 'J' || c == 'K' || c == 'L') { > ret += "5"; > } else if (c == 'M' || c == 'N' || c == 'O') { > ret += "6"; > } else if (c == 'P' || c == 'R' || c == 'S') { > ret += "7"; > } else if (c == 'T' || c == 'U' || c == 'V') { > ret += "8"; > } else if (c == 'W' || c == 'Y' || c == 'X') { > ret += "9"; > } > } > > } > > return ret.substring(0, 3) + "-" + ret.substring(3); > } > > public static void main(String[] args) throws Exception { > > Map<String, Integer> map = new HashMap<String, Integer>(); > > BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); > > String line = stdin.readLine(); > > int n = Integer.parseInt(line); > > for (int i = 0; i < n; i++) { > line = stdin.readLine(); > > String result = convert(line); > > if (map.get(result) == null) { > map.put(result, 1); > } else { > int newValue = map.get(result) + 1; > map.put(result, newValue); > } > } > > if (map.keySet().size() == n) { > System.out.println("No duplicates."); > } else { > > List<Count> list = new ArrayList<Count>(); > for (Map.Entry<String, Integer> entry : map.entrySet()) { > if (entry.getValue() > 1) { > > list.add(new Count(entry.getKey(), entry.getValue())); > } > } > > > Collections.sort(list, new Comparator() { > > public int compare(Object arg0, Object arg1) { > Count c1 = (Count) arg0; > Count c2 = (Count) arg1; > > return c1.s.compareTo(c2.s); > } > > }); > > > for (Count c: list) { > System.out.println(c.s + " " + c.number); > } > } > > } > > static class Count { > > String s; > int number; > > public Count(String s, int number) { > this.s = s; > this.number = number; > } > } > > } > > Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator