这个题目主要是涉及进行电话号码的转换,使用TreeMap来对电话号码进行统计以及排序。
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class Main {
private static Map<Character,Character> map = new HashMap<Character, Character>();
static {
map.put('A','2');
map.put('B','2');
map.put('C','2');
map.put('D','3');
map.put('E','3');
map.put('F','3');
map.put('G','4');
map.put('H','4');
map.put('I','4');
map.put('J','5');
map.put('K','5');
map.put('L','5');
map.put('M','6');
map.put('N','6');
map.put('O','6');
map.put('P','7');
map.put('R','7');
map.put('S','7');
map.put('T','8');
map.put('U','8');
map.put('V','8');
map.put('W','9');
map.put('X','9');
map.put('Y','9');
}
public static void main(String args[]) throws Exception
{
Scanner cin=new Scanner(System.in);
int count = cin.nextInt();
cin.nextLine();
Map<String,Integer> phoneToCountMap = new TreeMap<String, Integer>();
for(int idx = 0;idx < count;idx++){
String rawPhone = cin.nextLine();
String standardPhone = convertToStandardPhone(rawPhone);
int phoneCount = 0;
if(phoneToCountMap.containsKey(standardPhone)){
phoneCount = phoneToCountMap.get(standardPhone);
}
phoneCount++;
phoneToCountMap.put(standardPhone,phoneCount);
}
int dupCount = 0;
for (Map.Entry<String, Integer> stringIntegerEntry : phoneToCountMap.entrySet()) {
int phoneCount = stringIntegerEntry.getValue();
if(phoneCount > 1){
dupCount++;
System.out.println(stringIntegerEntry.getKey()+" "+stringIntegerEntry.getValue());
}
}
if(dupCount == 0){
System.out.println("No duplicates.");
}
}
private static String convertToStandardPhone(String rawPhone){
char[] charArray = rawPhone.toCharArray();
StringBuilder builder = new StringBuilder("");
for (char c:charArray){
if(c == '-'){
continue;
}
if(map.containsKey(c)){
c = map.get(c);
}
if(builder.length() == 3){
builder.append('-');
}
builder.append(c);
}
return builder.toString();
}
}
分享到:
相关推荐
487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 102808 Accepted: 17241 Description Businesses like to have memorable telephone numbers. One way to make a telephone number ...
East Central North America 1999。50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50字50...
北大POJ1002-487-3279【Hash+Qsort】 解题报告+AC代码
业余爱好。所以,算法不一定好,CODING也不一定佳,效率不一定高,只是能通过online judge而已。
如题所示,亲测可用。电话号码查重,不会的同学可以参考下,会做的同学可以给挑挑毛病!大家以代码会友!
POJ3211--Washing Clothes
POJ水题集-----50道左右-----增加自信啊..
POJ 1038--Bugs Integrated
POJ3036--Honeycomb Walk
POJ---1456.Supermarket测试数据及答案,题目描述:A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an integral ...
北大POJ初级题-数据结构:解题报告+AC代码
poj题目分类--acmer做题极有用资源
POJ3259--Wormholes,使用bellman方法。
poj 1690 Your-Term-Project.md
poj 1240 Pre-Post-erous!.md
北大POJ3414-Pots 解题报告+AC代码
北大POJ2002-Squares 解题报告+AC代码
北大POJ3253-POJ3253-Fence Repair【STL优先队列】 解题报告+AC代码
poj 2827 Auto-Calculation Machine.md