//思路: // 用一个二维数组telNumbers[100000][9]来存储全部的电话号码,每一行存储一个电话号码的标准表示。每读一个电话号码,首先将其准换成标准表示,然后存储到二维数 // 组telNumbers中。全部电话号码都输入完毕后,将数组telNumbers作为一个一维数组,其中每个元素十一个字符传,用C/C++提供的函数模板sort对其进行排序。用字符 // 串比较函数strcmp比较telNumbers中相邻的电话号码,判断是否有重复的电话号码,并计算重复次数。 #include <stdio.h> #include <stdlib.h> #include <string.h> char map[] = "22233344455566677778889999"; char str[80], telNumbers[100000][9]; int compare(const void *elem1, const void *elem2){//为函数模板sort定义数组元素的比较函数 return (strcmp((char *)elem1, (char *)elem2)); }; void standardizeTel(int n){ int j, k; j = k = -1; while (k < 8){ j++; if (str[j] == '-') continue; k++; if (k == 3){ telNumbers[n][k] = '-'; k++; } if (str[j] >= 'A' && str[j] <= 'Z'){ telNumbers[n][k] = map[str[j]-'A']; continue; } telNumbers[n][k] = str[j]; } telNumbers[n][k] = '\0'; return; } int main() { int n, i, j; bool noduplicate; scanf("%d", &n); for (i = 0; i < n; i++){ //输入电话号码存储倒数组telNumbers中 scanf("%s", str); standardizeTel(i); //将str中的电话号码转换成标准形式,存储在telNumbers的第i行 } qsort (telNumbers, n, 9, compare); //对输入的电话号码进行排序 noduplicate = true; i = 0; while (i < n){ //搜索重复的电话号码,并进行输出 j = i; i++; while (i < n && strcmp(telNumbers[i], telNumbers[j]) == 0) i++; if (i - j > 1){ printf("%s %d\n", telNumbers[j], i-j); noduplicate = false; } } if (noduplicate) printf("No duplicates.\n"); return 0; }