这个题比较坑的地方应该就是那个读入的号码长度,还有那个容易超时,用系统的快排就好了.也有人使用字典树的.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define LEN 9
#define BUFLEN 300
char trans[25] = {
'2','2','2',
'3','3','3',
'4','4','4',
'5','5','5',
'6','6','6',
'7','7','7','7',
'8','8','8',
'9','9','9',};
int mycomp(const void *t1, const void *t2);
void output(char tele[], int times);
int out = 0;
int main(void)
{
int n;
while(scanf("%d", &n) != EOF && n > 0)
{
char tele[n][LEN];
memset(tele, 0, sizeof(tele));
char buff[BUFLEN] = {0};
int i, j, k;
//input n tele(s).
for(i = 0; i < n; i++)
{
scanf("%s", buff);
//for every telephone number.
//standardize.
for(j = 0, k = 0; k < 7; j++)
{
if(isupper(buff[j]) && buff[j] != 'Q' && buff[j] != 'Z')
{
tele[i][k++] = trans[buff[j] - 'A'];
}
else if(isdigit(buff[j]))
{
tele[i][k++] = buff[j];
}
}
tele[i][k] = 0; //end the str.
}
//sort.
qsort(tele, n, LEN*sizeof(char), mycomp);
int crrc = 1;
for(i = 1; i < n; i++)
{
if(strcmp(tele[i-1], tele[i]) != 0){
if(crrc > 1)
output(tele[i-1], crrc);
crrc = 1;
}else{ //the same.
crrc++;
}//renew the record.
}
if(crrc > 1){
output(tele[n-1], crrc);
}//check for the last one.
if(out == 0){ //no output.
printf("No duplicates.\n");
}
}
return 0;
}
int mycomp(const void *t1, const void *t2)
{
char *p1 = (char *)t1;
char *p2 = (char *)t2;
if(strcmp(p1 ,p2) == 0) return 0;
else if(strcmp(p1, p2) > 0) return 1;
else return -1;
}
void output(char tele[], int times)
{
int i;
for(i = 0; i < 3; i++)
{
putchar(tele[i]);
}
putchar('-');
for(i = 3; i < 7; i++)
{
putchar(tele[i]);
}
putchar(' ');
printf("%d", times);
putchar('\n');
out++;
}