| ||||||||||
| 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 | |||||||||
谁抽空帮忙看下import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
boolean has = false;
int n = 0;
String phoneNo = null;
Map<String,Integer> hashMap = new TreeMap<String, Integer>();
Scanner cin = new Scanner(System.in);
n = cin.nextInt();
for(int i = 0;i < n; ++i){
phoneNo = cin.next();
//先去掉所有的 "-"
phoneNo = phoneNo.replaceAll("-", "");
//替换成对应的数字
phoneNo = phoneNo.replaceAll("A|B|C+", "2");
phoneNo = phoneNo.replaceAll("D|E|F+", "3");
phoneNo = phoneNo.replaceAll("G|H|I+", "4");
phoneNo = phoneNo.replaceAll("J|K|L+", "5");
phoneNo = phoneNo.replaceAll("M|N|O+", "6");
phoneNo = phoneNo.replaceAll("P|R|S+", "7");
phoneNo = phoneNo.replaceAll("T|U|V+", "8");
phoneNo = phoneNo.replaceAll("W|X|Y+", "9");
phoneNo = phoneNo.replaceAll("Q|Z+", "");
Integer count = hashMap.get(phoneNo);
if(count != null){
int c = count.intValue();
hashMap.put(phoneNo, c + 1);
}else{
hashMap.put(phoneNo, 1);
}
}
Set<Entry<String, Integer>> entrys = hashMap.entrySet();
Iterator<Entry<String, Integer>> it = entrys.iterator();
while(it.hasNext()){
Entry<String, Integer> entry = it.next();
int count = entry.getValue();
String no = entry.getKey();
if(count > 1){
System.out.println(no.substring(0,3) + "-" + no.substring(3) + " " + count);
has = true;
}
}
if(!has){
System.out.println("No duplicates.");
}
}
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator