开始用字符串TLE,后来改成整数AC。先排序,再计数。
代码:
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 5 typedef struct 6 { 7 int num; 8 int count; 9 }node; 10 11 int cmp(const void *a, const void *b) 12 { 13 node *pa=(node *)a; 14 node *pb=(node *)b; 15 return pa->num-pb->num; 16 } 17 18 int main() 19 { 20 int T, n, i, j, flag=0; 21 char s[50]; 22 node tel[100010]; 23 scanf("%d", &T); 24 while(T--) 25 { 26 scanf("%d%*c", &n); 27 for (i=0; i<n; i++) 28 { 29 tel[i].num=0; 30 tel[i].count=1; 31 gets(s); 32 for (j=0; j<strlen(s); j++) 33 { 34 if (s[j] != '-') //可以这样 int b[]={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,9}; 35 { // 36 if (s[j]>='0' && s[j]<='9') //下面的就可一简写很多了。 37 tel[i].num=tel[i].num*10+s[j]-'0'; 38 if (s[j]=='A' || s[j]=='B' || s[j]=='C') 39 tel[i].num=tel[i].num*10+2; 40 if (s[j]=='D' || s[j]=='E' || s[j]=='F') 41 tel[i].num=tel[i].num*10+3; 42 if (s[j]=='G' || s[j]=='H' || s[j]=='I') 43 tel[i].num=tel[i].num*10+4; 44 if (s[j]=='J' || s[j]=='K' || s[j]=='L') 45 tel[i].num=tel[i].num*10+5; 46 if (s[j]=='M' || s[j]=='N' || s[j]=='O') 47 tel[i].num=tel[i].num*10+6; 48 if (s[j]=='P' || s[j]=='R' || s[j]=='S') 49 tel[i].num=tel[i].num*10+7; 50 if (s[j]=='T' || s[j]=='U' || s[j]=='V') 51 tel[i].num=tel[i].num*10+8; 52 if (s[j]=='W' || s[j]=='X' || s[j]=='Y') 53 tel[i].num=tel[i].num*10+9; 54 } 55 } 56 } 57 qsort(tel, n, sizeof(node), cmp); //快排,如果不用可能会TLE。 58 flag=0; 59 for (i=0; i<n-1;) 60 { 61 for (j=i+1; j<n; j++) 62 { 63 if (tel[i].num==tel[j].num) 64 { 65 tel[i].count++; 66 } 67 else 68 break; 69 } 70 if (tel[i].count>1) 71 { 72 flag=1; 73 printf("%03d-%04d %d\n", tel[i].num/10000,tel[i].num%10000,tel[i].count); 74 } 75 i=j; 76 } 77 if (!flag) 78 printf("No duplicates.\n"); 79 if (T) 80 printf("\n"); 81 } 82 return 0; 83 }