C++学习日记——excel排序

来源:互联网 发布:js 函数添加到window 编辑:程序博客网 时间:2024/05/18 01:49
题目描述:
    Excel可以对一组纪录按任意指定列排序。现请你编写程序实现类似功能。
    对每个测试用例,首先输出1行“Case i:”,其中 i 是测试用例的编号(从1开始)。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3 
时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。

#include <stdio.h>#include <algorithm>#include <string.h>using namespace std;struct student{int id;char name[20];int score;}buf[100000];bool cmp1(student a,student b){return a.id<b.id;}bool cmp2(student a,student b){int tmp=strcmp(a.name,b.name);if(tmp!=0) return tmp<0;else return a.id<b.id;}bool cmp3(student a,student b){if(a.score!=b.score) return a.score<b.score;else return a.id<b.id;}int main(void){int n,c;while((scanf("%d%d",&n,&c))==2&&(n!=0)){for(int i=0;i<n;++i)scanf("%d%s%d",&buf[i].id,&buf[i].name,&buf[i].score);switch(c){case 1:sort(buf,buf+n,cmp1);break;case 2:sort(buf,buf+n,cmp2);break;case 3:sort(buf,buf+n,cmp3);break;default:break;}printf("Case %d:\n",c);for(int i=0;i<n;++i)printf("%06d %s %d\n",buf[i].id,buf[i].name,buf[i].score);}return 0;}


0 0