对于排序来说,c++的sort函数用着还是相当方便的,比什么冒泡排序,快速排序简单方便了很多。
sort函数的头文件是#include<algorithm>
sort函数默认的排序方式是升序排序,即从小到大。
sort函数的使用
简单来说就是sort(begin,end,cmp);
对于cmp函数一会再说,begin就是开始排序的数字,end就是排序最后那个数;
如:给你一系列数,从小到大输出出来
for(i =0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
for(i = 0;i<n;i++)
printf("%d",a[i]);
可能有很多情况下我们需要降序排序,这个时候就是要用到cmp函数了。
给你一系列数,想要从大到小输出;
bool cmp(int a,int b)
{
return a>b;
}
for(i =0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n,cmp);
for(i = 0;i<n;i++)
printf("%d",a[i]);
总之,cmp函数用bool说明了他需要的只是返回0或1而已。而上面的函数是return a>b,所以当a>b时返回1,a<b时返回0;
归根到底还是什么时候返回1或0;
如果改成return a<b,所以当a<b时返回1,a>b时返回0,就成为升序排序了。
一般来说,涉及到结构体排序的时候,经常使用sort排序,因为真的是非常方便。
比如说这一题:http://acm.hdu.edu.cn/showproblem.php?pid=1234
开门人和关门人Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19300 Accepted Submission(s): 9777 Problem Description 每天第一个到机房的人要把门打开,最后一个离开的人要把门关好。现有一堆杂乱的机房签
Input 测试输入的第一行给出记录的总天数N ( > 0 )。下面列出了N天的记录。
Output 对每一天的记录输出1行,即当天开门和关门人的证件号码,中间用1空格分隔。
Sample Input 3 1 ME3021112225321 00:00:00 23:59:59 2 EE301218 08:05:35 20:56:35 MA301134 12:35:45 21:40:42 3 CS301111 15:30:28 17:00:10 SC3021234 08:00:00 11:25:25 CS301133 21:45:00 21:58:40
Sample Output ME3021112225321 ME3021112225321 EE301218 MA301134 SC3021234 CS301133 |
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct student{ //设置结构体,储存数据
char name[16]; //学号
char co[10]; //来的时间
char go[10] ; //走的时间
}a[1000];
bool cmp1(student a,student b) //将来的时间按从小到大排序
{
int n=strcmp(a.co,b.co); //strcmp函数,比较字符串的大小的
if(n==-1)
return 1;
return 0;
}
bool cmp2(student a,student b) //将走的时间按从大到小排序
{
int n=strcmp(a.go,b.go);
if(n==1)
return 1;
return 0;
}
int main()
{
int n,m;
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
for(int i=0;i<m;i++)
{
scanf("%s%s%s",a[i].name,&a[i].co,a[i].go);
}
sort(a,a+m,cmp1);
printf("%s ",a[0].name);
sort(a,a+m,cmp2);
printf("%s\n",a[0].name);
}
return 0;
}
http://acm.hdu.edu.cn/showproblem.php?pid=1862
EXCEL排序Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 23225 Accepted Submission(s): 8347 Problem Description Excel可以对一组纪录按任意指定列排序。现请你编写程序实现类似功能。
Input 测试输入包含若干测试用例。每个测试用例的第1行包含两个整数 N (<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号。以下有 N
Output 对每个测试用例,首先输出1行“Case i:”,其中 i 是测试用例的编号(从1开始)。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3
Sample Input 3 1 000007 James 85 000010 Amy 90 000001 Zoe 60 4 2 000007 James 85 000010 Amy 90 000001 Zoe 60 000002 James 98 4 3 000007 James 85 000010 Amy 90 000001 Zoe 60 000002 James 90 0 0
Sample Output Case 1: 000001 Zoe 60 000007 James 85 000010 Amy 90 Case 2: 000010 Amy 90 000002 James 98 000007 James 85 000001 Zoe 60 Case 3: 000001 Zoe 60 000007 James 85 000002 James 90 000010 Amy 90 |
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct stu{
char num[7];
char name[10];
int sum;
}a[100005];
bool cmp1(stu a,stu b)
{
int n=strcmp(a.num,b.num);
if(n==1)
return 0;
else
return 1;
}
bool cmp2(stu a,stu b)
{
int n=strcmp(a.name,b.name);
if(n==1)
return 0;
else if(n==0)
{
int m=strcmp(a.num,b.num);
if(m==1)
return 0;
else
return 1;
}
else
return 1;
}
bool cmp3(stu a,stu b)
{
if(a.sum>b.sum)
return 0;
else if(a.sum==b.sum)
{
int x=strcmp(a.num,b.num);
if(x==1)
return 0;
else
return 1;
}
else
return 1;
}
int main()
{
int n,c;
int cnt=0;
while(scanf("%d%d",&n,&c)&&n)
{
for(int i=0;i<n;i++)
{
scanf("%s%s%d",a[i].num,a[i].name,&a[i].sum);
}
if(c==1)
{
sort(a,a+n,cmp1);
}
else if(c==2)
{
sort(a,a+n,cmp2);
}
else if(c==3)
{
sort(a,a+n,cmp3);
}
cnt++;
printf("Case %d:\n",cnt);
for(int i=0;i<n;i++)
{
printf("%s %s %d\n",a[i].num,a[i].name,a[i].sum);
}
}
return 0;
}
萌新一个,可能会有错误,可以在评论指出。