对二维字符数组排序(2sort和1qsort)
来源:互联网 发布:帝国cms 整站 编辑:程序博客网 时间:2024/05/16 04:53
转自:http://www.cnblogs.com/submarine/archive/2010/04/22/1941254.html
sort,用到了结构体
第一种sort:
282MS
代码
#include<stdio.h>
#include<algorithm>
using namespace std;
struct Array
{
char data[30];
}dna[20005];
bool operator < (Array const& arr1, Array const& arr2)
{
return strcmp(arr1.data, arr2.data) < 0;
}
int main()
{
int n, m, i, count, num[20005];
while(scanf("%d%d", &n, &m) != EOF)
{
if(!n && !m)
return 0;
for(i=0; i<=n; i++)
num[i] = 0;
for(i=0; i<n; i++)
scanf("%s", dna[i].data);
sort(dna, dna+n);
count = 1;
for(i=1; i<=n; i++)
if(strcmp(dna[i-1].data, dna[i].data) == 0)
count++;
else
{
num[count]++;
count = 1;
}
for(i=1; i<=n; i++)
printf("%d\n", num[i]);
}
return 0;
}
第二种sort:
407MS
代码
#include<stdio.h>
#include<algorithm>
using namespace std;
struct Array
{
char date[30];
}dna[20005];
bool cmp(Array arr1, Array arr2)
{
return strcmp(arr1.date, arr2.date) < 0;
}
int main()
{
int n, m, i, count, num[20005];
while(scanf("%d%d", &n, &m) != EOF)
{
if(!n && !m)
return 0;
for(i=0; i<=n; i++)
num[i] = 0;
for(i=0; i<n; i++)
scanf("%s", dna[i].date);
sort(dna, dna+n, cmp);
count = 1;
for(i=1; i<=n; i++)
if(strcmp(dna[i-1].date, dna[i].date) == 0)
count++;
else
{
num[count]++;
count = 1;
}
for(i=1; i<=n; i++)
printf("%d\n", num[i]);
}
return 0;
}
qsort:
329MS
代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int cmp(const void* a, const void* b)
{
return (strcmp((char* )a,(char* )b));
}
int main()
{
int n, m, i, count, num[20005];
char dna[20005][30];
while(scanf("%d%d", &n, &m) != EOF)
{
if(!n && !m)
return 0;
for(i=0; i<=n; i++)
num[i] = 0;
for(i=0; i<n; i++)
scanf("%s", dna[i]);
qsort(dna, n, sizeof(dna[0]), cmp);
count = 1;
for(i=1; i<=n; i++)
if(strcmp(dna[i-1], dna[i]) == 0)
count++;
else
{
num[count]++;
count = 1;
}
for(i=1; i<=n; i++)
printf("%d\n", num[i]);
}
return 0;
}
/**************************************************************************
原文来自博客园——Submarinex的博客: www.cnblogs.com/submarine/
*************************************************************************/
0 0
- 对二维字符数组排序(2sort和1qsort)
- 对二维字符数组排序(2SORT和1QSORT)
- 对二维字符数组排序(2sort和1qsort)
- sort和qsort对字符数组排序
- sort和qsort对字符数组排序
- sort对二维字符数组排序
- sort对二维字符数组排序
- qsort对二维数组的排序
- qsort对动态二维数组进行排序
- 用qsort对二维数组进行排序
- 使用qsort对二维字符数组排序疑难问题调试及解决过程
- qsort()实现对字符数组的排序
- 1214 1159,二维字符数组sort排序
- 用sort对二维数组排序
- sort对二维数组进行排序备忘
- sort对二维char数组排序?
- sort和qsort排序
- sort和qsort排序
- Ajax简介
- ubuntu下如何用命令行运行deb安装包
- 队列
- Acegi框架介绍
- Acegi (Spring Security)入门
- 对二维字符数组排序(2sort和1qsort)
- 2014年誓言:干掉网页设计程序——Dreamweaver!
- 用sort给字符串排序
- 报数游戏(2)--【英雄会】
- hdu 1113 Word Amalgamation
- pig安装
- C++学习随笔
- (七)装饰模式
- async笔记(二)——process