UVA - 755 487–3279
题目大意:题目给定 n 串字符串,包括0-9,A-Z 和 ‘-’ ,求其对应的号码,并按字典序从小到大排序,输出出现次数大于等于2的号码,格式为xxx-xxxx。如果没有结果 No duplicates.
解题思路: 我是字符串读入然后再将所有的转换成xxx-xxxx的字符串形式,然后用qsort排序,然后累加重复的号码输出即可
#include <cstdio>
#include <cstring>
#include <algorithm>
int cmp ( const void *_a, const void *_b) {
char *a = (char*)_a;
char *b = (char*)_b;
return strcmp(a,b);
}
int main() {
int N;
int flag;
int t;
int M;
int ssss = 1;
int bb[] = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 0, 7, 7, 8, 8, 8, 9, 9, 9, 0};
char a[10000][100];
char b[10000][10];
int c[10000];
scanf("%d", &N);
getchar();
while (N--) {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
if (ssss != 1) printf("\n");
ssss++;
scanf("%d", &M);
for (int i = 0; i < M; i++) {
t = 7;
scanf("%s", a[i]);
for (int j = strlen(a[i]); j >= 0; j--) {
int sssss = a[i][j] - 'A';
if (t == 3) t--;
b[i][3] = '-';
if ( a[i][j] >= 'A' && a[i][j] <= 'Z') {
b[i][t] = '0' + bb[sssss];
t--;
}else
if (a[i][j] >= '0' && a[i][j] <= '9') {
b[i][t] = a[i][j];
t--;
}
}
if (t < 2)
t++;
if ( t >= 0)
for (int j = 0; j < t; j++) {
if ( j == 3) {
j++;
t++;
}
b[i][j] = '0';
}
}
qsort (b, M, 10, cmp);
int ttttt = 1;
flag = 0;
for (int i = 0; i < M; i++) {
if (strcmp(b[i], b[i+1]) == 0 )
ttttt++;
else
if (ttttt > 1 ) {
flag = 1;
printf("%s %d\n", b[i], ttttt);
ttttt = 1;
}
}
if (!flag)
printf("No duplicates.\n");
}
}