您的位置:首页 > 其它

poj1002 487-3279

2016-05-29 23:04 288 查看
http://poj.org/problem?id=1002

贴一发老外写的代码:

。我一开始写了一百多行,然后WA了。

借鉴一下map映射技巧,qsort排序技巧。

<span style="font-size:18px;">#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char map[26] = {'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 ph[100000][9];
int nph;
char buf[1000];
int main()
{
int i, j, x, z;
memset( ph, 0, sizeof(ph) );
scanf( " %d", &nph ); /*电话号码数量*/
for( i = 0; i < nph; i++ )
{
scanf( " %s", buf );
x = 0;
for( j = 0; buf[j]; j++ ) /*遍历原号码*/
{
if( buf[j] == '-' ) continue; /*遇到连接符跳过*/
if( buf[j] >= 'A' && buf[j] <= 'Z' ) /*遇到大写字母映射*/
buf[j] = map[buf[j]-'A'];
ph[i][x++] = buf[j]; /*将标准格式储存在二位数组ph[][]中*/
if( x == 3 )
ph[i][x++] = '-';
}
}
qsort( ph, nph, 9, strcmp ); /*对字符串二位数组ph[][]快速排序*/
x = 1;
z = 0;
for( i = 1; i < nph; i++ ) /*从第2个标准号码开始遍历*/
{
if( strcmp( ph[i-1], ph[i] ) ) /*当前标准号码与前一个标准号码不相同*/
{
if( x > 1 ) /*前一个标准号码不只一个*/
{
printf( "%s %d\n", ph[i-1], x );
z = 1;
}
x = 1; //重置
}
else /*当前标准号码与前一个标准号码相同*/
{
x++; //累加统计相同标准号码的个数
}
}
if( x > 1 )
{
printf( "%s %d\n", ph[i-1], x );
z = 1;
}
if( !z ) /*如果输入数据中没有重复的号码*/
printf( "No duplicates.\n" );
}</span>

小结:
一.qsort:编译器函数库自带的快速排序函数。

二.头文件:stdlib.h

三.函数原型:void qsort(void* base, int nelem, int width,int (*fcmp)(const void*,const void*))

参数 : (1).待排序数组首地址

          (2).数组中待排序元素数量

          (3).各元素的占用空间大小

          (4).指向函数的指针,用于确定排序的顺序

四.简单举例:1.一维数组排序:

                         int a[1000];

                         qsort(a, 1000, sizeof(int), comp);

                         int comp(const void* a,const void* b)

                {

                             return *(int*)a - *(int*)b;

                        }    

                     2.二位数组排序:

                        int a[1000][2];

                        qsort(a, 1000, sizeof(int)*2, comp);

                    3.字符串排序:

                       char a[MAX_1][MAX_2];

                       initial(a);

                       qsort(a, length, sizeof(a[0]), comp); //length为数组a的长度

五.回顾:

    strcmp(字符数组名1,字符数组名2)

    字符串1 > 字符串2, 函数返回值 > 0;

    字符串1 < 字符串2,函数返回值 < 0;

    字符串1 = 字符串2,函数返回值 = 0;                    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: