本题需注意以下三点:
1、读入的字符串可能包含多个“-”,如果开数组到10明显不够,尽量开到30以上
2、只输出出现多于1次的电话号码
3、没有符合条件的电话号码时要输出“No duplicates.”
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
using namespace std;
int cmp(const void *_x,const void *_y)
{
char *x=(char *)_x;
char *y=(char *)_y;
return strcmp(x,y);
}
int change(char x)
{
if(isdigit(x))return x;
else if(x=='A'||x=='B'||x=='C')return '2';
else if(x=='D'||x=='E'||x=='F')return '3';
else if(x=='G'||x=='H'||x=='I')return '4';
else if(x=='J'||x=='K'||x=='L')return '5';
else if(x=='M'||x=='N'||x=='O')return '6';
else if(x=='P'||x=='R'||x=='S')return '7';
else if(x=='T'||x=='U'||x=='V')return '8';
else if(x=='W'||x=='X'||x=='Y')return '9';
else return -1;
}
int main()
{
int n;
char s[100];
char tl[100005][20];
memset(tl,0,sizeof(tl));
cin>>n;
getchar();
for(int i=0; i<n; i++)
{
gets(s);
int l=strlen(s);
int c=0;
for(int j=0; j<l; j++)
{
if(change(s[j])==-1)continue;
else
s[c++]=change(s[j]);
}
s[c]='\0';
strcpy(tl[i],s);
}
qsort(tl,n,sizeof(tl[0]),cmp);
int k=0,f=0;
for(int i=1; i<=n; i++)
{
if(strcmp(tl[i],tl[i-1])!=0)
{
if(i-k>1)
{
f++;
for(int j=0; tl[i-1][j]!='\0'; j++)
{
if(j==3)cout<<"-";
cout<<tl[i-1][j];
}
cout<<" "<<i-k<<endl;
}
k=i;
}
}
if(f==0)cout<<"No duplicates."<<endl;
return 0;
}