#include
#include
#include
using namespace std;
char map[] = "22233344455566677778889999";
char strIn[100], telNumbers[100001][9];
int compare (const void *elem1, const void *elem2) //比较相邻字符串是否相等
{
return (strcmp((char *)elem1, (char *)elem2));
}
void standardizeTel(int n) //字符串数字号码标准化
{
int telNumIndex, stdTelNumIndex;
telNumIndex = stdTelNumIndex = -1;
while (stdTelNumIndex < 8)
{
telNumIndex ++;
if (strIn[telNumIndex] == '-')
continue;
stdTelNumIndex ++;
if (stdTelNumIndex == 3)
{
telNumbers[n][stdTelNumIndex] = '-';
stdTelNumIndex ++;
}
if (strIn[telNumIndex] >= 'A' && strIn[telNumIndex] <= 'Z')
{
telNumbers[n][stdTelNumIndex] = map[strIn[telNumIndex] - 'A'];
continue;
}
telNumbers[n][stdTelNumIndex] = strIn[telNumIndex];
}
telNumbers[n][8] = '\0';
}
int main()
{
int n, i, j;
bool noDuplicate; //布尔值判断是否出现相同号码
cin >> n;
for (i = 0; i < n; i ++)
{
cin >> strIn;
standardizeTel(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)
{
cout << telNumbers[j] << " " << (i - j) << endl; //输出相同号码的个数
noDuplicate = false;
}
}
if (noDuplicate)
cout << "No duplicates." << endl;
system("pause");
return 0;
}
不懂请追问
我做的要O(n^2)
转①哥们poker2008的只有O(n),想法不错
我修改成C++了
CODE:
#include
#include
using namespace std;
int stdTelNum[10000000];
int main()
{
int nCase, val, unit;
bool noDuplicate = true;
char str[100];
memset(stdTelNum, 0, sizeof(stdTelNum));
cin >> nCase;
while (nCase --)
{
cin >> str;
val = 0;
for(int i = 0; i < strlen(str); i ++) //亮点:把每一个字符串对应一个数字,想法自然
{ //算法简单,快速 缺点空间换时间
if (str[i] >= 'A' && str[i] <= 'P')
{
unit = (str[i] - 'A') / 3 + 2;
val = 10 * val + unit;
}
else if (str[i] >= 'R' && str[i] <= 'Y')
{
unit = (str[i] - 'A' - 1) / 3 + 2 ;
val = 10 * val + unit;
}
else if (str[i] >= '0' && str[i] <= '9')
{
unit = str[i] - '0';
val = 10 * val + unit;
}
}
//printf("%07d\n",c);
stdTelNum[val] ++;
}
for(int i = 0; i < 10000000; i ++)
{
if(stdTelNum[i] >= 2)
{
cout << (i / 10000) << "-" << (i % 10000) << " " << stdTelNum[i] << endl;
noDuplicate = false;
}
}
if (noDuplicate)
cout << "No duplicates." << endl;
system("pause");
return 0;
}
建议你的程序要完成如下任务:
首先:无效符号删除,将空格、减号键、下划线及其他符号去除;
其次:将所有字符转换为对应数字;
第三:检查数字是否7位的,是否符合电话号码格式;
第四:统计各电话号码出现次数
按以上方法一步一步来,很容易的,祝你好运!