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;
贴一发老外写的代码:
。我一开始写了一百多行,然后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;
相关文章推荐
- 菜鸟更要独立思考之二 改进一个不使用wait和notify顺序打印ABC 10次的算法
- java笔记一
- 汉诺塔python
- MAC 软件清单
- iOS UITableView 快速滚动(索引方式实现)
- 《Python数据分析常用手册》一、NumPy和Pandas篇
- 第二轮冲刺-Runner站立会议07
- 针对无线信道衰落特性分析3G,4G,5G的关键技术异同点
- UIActionSheet的使用
- 简单的oracle存储过程
- 第二阶段冲刺5
- JavaScript
- Java中static的运用
- Git的基本使用1—创建版本库及简单命令
- Ng机器学习 Week6
- 拦截malloc、free等库函数(malloc钩子)
- linux内核container_of宏获取结构体地址
- iOS-库 .a与.framework
- FISTA的由来:从梯度下降法到ISTA & FISTA
- 数据结构笔记整理第6章:图