problem : http://acm.pku.edu.cn/JudgeOnline/problem?id=1002
source :
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
char telenum[100000][8] = {0};
char change(char m)
{
char ret = 0;
switch(m)
{
case 'A':
case 'B':
case 'C':
ret = '2';
break;
case 'D':
case 'E':
case 'F':
ret = '3';
break;
case 'G':
case 'H':
case 'I':
ret = '4';
break;
case 'J':
case 'K':
case 'L':
ret = '5';
break;
case 'M':
case 'N':
case 'O':
ret = '6';
break;
case 'P':
case 'R':
case 'S':
ret = '7';
break;
case 'T':
case 'U':
case 'V':
ret = '8';
break;
case 'W':
case 'X':
case 'Y':
ret = '9';
break;
case '-':
ret = 0;
break;
default:
ret = m;
break;
}
return ret;
}
int compare(const void *arg1, const void *arg2)
{
return strcmp((char *)arg1, (char *)arg2);
}
int main()
{
unsigned int n;
unsigned int i, j, k;
int m;
int flag;
char tmp;
char memorable[100] = {0};
scanf("%d\n", &n);
//printf("%d\n",n);
for (i = 0; i < n; ++i)
{
gets(memorable);
for (j = 0, k = 0; j < strlen(memorable); ++j)
{
if (tmp = change(memorable[j]))
{
telenum[i][k++] = tmp;
}
}
}
qsort(telenum, n, 8, compare);
for (i = 1, m = 1, flag = 1; i < n; ++i)
{
if (0 == strcmp(telenum[i], telenum[i - 1]))
{
m++;
}
else
{
if (m > 1)
{
printf("%c%c%c-%c%c%c%c %d\n",
telenum[i - 1][0],telenum[i - 1][1],telenum[i - 1][2],
telenum[i - 1][3],telenum[i - 1][4],telenum[i - 1][5],telenum[i - 1][6],
m);
m = 1;
flag = 0;
}
}
}
if (0 == strcmp(telenum[i - 2], telenum[i - 1]) && m > 1)
{
printf("%c%c%c-%c%c%c%c %d\n",
telenum[i - 1][0],telenum[i - 1][1],telenum[i - 1][2],
telenum[i - 1][3],telenum[i - 1][4],telenum[i - 1][5],telenum[i - 1][6],
m);
flag = 0;
}
if (flag)
{
printf("No duplicates. ");
}
return 0;
}
本题完全是自己的代码,所以没有什么好讲的,本来想看看有没有用hash表来做的。但是后来发现还是用数组来做简单.
1.一开始当然的WA,WA得莫名其妙。
2.看了看,少了printf("No duplicates. "); 加上了最后的数字处理,还是WA,更加莫名其妙
3.最后把接受的数组定义到100,AC了...无语
posted on 2008-04-17 02:25
margin 阅读(480)
评论(1) 编辑 收藏 引用 所属分类:
C/C++