题目描述
Excel可以对一组纪录按任意指定列排序。现请你编写程序实现类似功能。
对每个测试用例,首先输出1行“Case i:”,其中 i 是测试用例的编号(从1开始)。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3 时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。
输入
测试输入包含若干测试用例。每个测试用例的第1行包含两个整数 N (N<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号。以下有N行,每行包含一条学生纪录。每条学生纪录由学号(6位数字,同组测试中没有重复的学号)、姓名(不超过8位且不包含空格的字符串)、成绩(闭区间[0, 100]内的整数)组成,每个项目间用1个空格隔开。当读到 N=0 时,全部输入结束,相应的结果不要输出。


输出
对每个测试用例,首先输出1行“Case i:”,其中 i 是测试用例的编号(从1开始)。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3 时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。


样例输入
4 1
000001 Zhao 75
000004 Qian 88
000003 Li 64
000002 Sun 90
4 2
000005 Zhao 95
000011 Zhao 75
000007 Qian 68
000006 Sun 85
4 3
000002 Qian 88
000015 Li 95
000012 Zhao 70
000009 Sun 95
0 3
样例输出
Case 1:
000001 Zhao 75
000002 Sun 90
000003 Li 64
000004 Qian 88
Case 2:
000007 Qian 68
000006 Sun 85
000005 Zhao 95
000011 Zhao 75
Case 3:
000012 Zhao 70
000002 Qian 88
000009 Sun 95
000015 Li 95

#include<iostream>
#include<algorithm>
#include<string>//这个是字符串容器
#include<stdio.h>
#include<string.h>//这个是包含与char数组相关的函数例如strcmp、strcpy等
#include<vector>
using namespace std;
int C;//这个用来判断准备使用哪个比较器
struct stu
{
char stunumber[20];//用string容器的话,scanf以%s的格式输入时需要用c_str()函数将字符串转成字符数组
char name[20];
int score;
/*bool operator<(stu temp1)
{
if (C == 1)
{
if (strcmp(stunumber, temp1.stunumber) >=0)
return 0;
else
return 1;

}
else if (C == 2)
{
if (!strcmp(name, temp1.name))
{
if (strcmp(stunumber, temp1.stunumber) >= 0)
return 0;
else
return 1;
}
else
{
if (strcmp(name, temp1.name) >= 0)
return 0;
else
return 1;
}
}
else if (C == 3)
{
if (score == temp1.score)
{
if (strcmp(stunumber, temp1.stunumber) >= 0)
return 0;
else
return 1;
}
else
{
if (score > temp1.score)
return 0;
else
return 1;
}
}
}*///不修改比较器的话就重载小于号,注意重载函数返回值的完备性,就是在合理的情况下都应该有返回值,否则报比较器错误
};
bool cmp1(stu temp1, stu temp2)
{
if (strcmp(temp1.stunumber,temp2.stunumber)>0)
return 0;
else return 1;
}//C==1时的比较器,下面依次是2、3时的比较器
bool cmp2(stu temp1, stu temp2)
{
if (strcmp(temp1.name, temp2.name)>0)
return 0;
else if (strcmp(temp1.name, temp2.name) == 0)
{
if (strcmp(temp1.stunumber, temp2.stunumber) > 0)
return 0;
else
return 1;
}
else return 1;
}
bool cmp3(stu temp1, stu temp2)
{
if (temp1.score>temp2.score)
return 0;
else if (temp1.score==temp2.score)
{
if (strcmp(temp1.stunumber, temp2.stunumber) > 0)
return 0;
else
return 1;
}
else return 1;
}
int main()
{
int N; vector<stu> s; stu temp; int testnum = 1;//注意题目要求,Case后跟的是样例号
while (scanf("%d",&N)!=EOF)
{
//cin >> C;
scanf("%d", &C);
if (N == 0)
break;
while (N--)
{
//cin >> temp.stunumber >> temp.name >> temp.score;
scanf("%s%s%d", temp.stunumber, temp.name, &temp.score);
s.push_back(temp);
}
switch (C)
{
case 1:sort(s.begin(), s.end(), cmp1); break;
case 2:sort(s.begin(), s.end(), cmp2); break;
case 3:sort(s.begin(), s.end(), cmp3); break;

default:
break;
}
// sort(s.begin(), s.end());
printf("Case %d:\n", testnum++);
//cout << "Case " << C << ":" << endl;
for (int i = 0; i < s.size(); i++)
{
printf("%s %s %d\n", s[i].stunumber, s[i].name, s[i].score);
//cout << s[i].stunumber << " " << s[i].name << " " << s[i].score << endl;
}
s.clear();
}
}
/*这题我失误在那个testnum上,还有对比较器的自定义上。只能重载小于号,否则报错*/