Description
The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows:
A, B, and C map to 2
D, E, and F map to 3
G, H, and I map to 4
J, K, and L map to 5
M, N, and O map to 6
P, R, and S map to 7
T, U, and V map to 8
W, X, and Y map to 9
There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010.
Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.)
Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number.
Input
Output
No duplicates.
Sample Input
12 4873279 ITS-EASY 888-4567 3-10-10-10 888-GLOP TUT-GLOP 967-11-11 310-GINO F101010 888-1200 -4-8-7-3-2-7-9- 487-3279
Sample Output
310-1010 2 487-3279 4 888-4567 3
简介:原题目名字是487-3249,吐槽一下,起的是啥破名,给人以不明觉厉的感觉。总之呢这个题就是一个字符串转换+排序的算法。先输入一个数代表字符串数量,然后每行输入一个字符串,是字母的话就按照规则转换成数字,不在规则内的按0处理,遇到‘-’跳过。最后呢按照格式输出所有转换后的有重复的字符串(要求排好序)以及重复次数,如果没有重复的就输出一个No duplicates。题目要求简单易懂,原理也并不复杂,但实现起来感觉需要了解的知识点还是蛮多的。这家伙给我整的心碎呐。总之不废话了,代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static char returnValue(char c) {
if (c <= 67 && c >= 65) {
return '2';
} else if (c <= 70 && c >= 68) {
return '3';
} else if (c <= 73 && c >= 71) {
return '4';
} else if (c <= 76 && c >= 74) {
return '5';
} else if (c <= 79 && c >= 77) {
return '6';
} else if (c <= 83 && c >= 80) {
return '7';
} else if (c <= 86 && c >= 84) {
return '8';
} else if (c <= 89 && c >= 87) {
return '9';
} else {
return '0';
}
}
public static String parseString(String a){
String temp="";
char[] s=a.toCharArray();
for(char s1:s){
if(s1=='-'){
continue;
}
else if(s1>='0'&&s1<='9'){
temp+=s1;
}
else{
s1=returnValue(s1);
temp+=s1;
}
}
return temp;
}
public static void main(String[] args) throws Exception{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
HashMap<String,Integer> m=new HashMap<String,Integer>();
int linenumber=Integer.parseInt(in.readLine());
while(linenumber--!=0){
String str=in.readLine();
str=parseString(str);
Integer times=m.get(str);
m.put(str,times==null?1:times+1);
}
boolean flag=false;
Set<String> a=new TreeSet<String>(m.keySet());
Iterator<String> it=a.iterator();
while(it.hasNext()){
String s=it.next();
int value=m.get(s);
if(value>1){
flag=true;
System.out.println(s.substring(0, 3)+"-"+s.substring(3, 7)+" "+value);
}
}
if(!flag){
System.out.println("No duplicates.");
}
}
}
必须说明一下,我的代码还是参考了网上别人的代码,因为自己写到一半发现排序啥的好麻烦,就上网看了看,然后发现用集合类解决就行(发现这个的时候我真想啪啪啪抽自己几耳光,自己以前也经常用,关于集合类的知识点也刚看过书没多久,怎么写起来就忘了呢?看来还是自己学以致用的能力不高)。代码整体思想就是先把输入读进一个HashMap中,然后把HashMap里的Key值传给一个TreeSet,利用TreeSet红黑树的内部特性实现有序排序,然后构造一个TreeSet的迭代器,遍历输出即可。当然还有字符串的处理过程,这个不难,字符串处理代码里还有foreach语法的运用,也不难。
总之从这个Poj的第二道入门级别的算法题上就能看出,即使是很简单很简单的题(这个题感觉都没用到啥算法),想写好也不容易。确实要多动手写代码,纸上谈兵还是不行。这个题让我对集合类的运用更加熟练了,感觉不错。