hdu1862

//开始把student stu[100000]放置在main()中导致栈溢出,所以必须放在全局位置,

//可以调用数组的排序函数sort,包含头文件#include<algorithm>,在默认的情况下,数组sort函数进行升序排序

//控制sort的第三个参数,传递函数指针进去,可以按照自己写的函数进行排序

#include<iostream>
#include<algorithm>
using namespace std;
class student{
public:
char number[7];
char name[9];
short mark;
};
student stu[100000];
bool cmp1(student x, student y);
bool cmp2(student x, student y);
bool cmp3(student x, student y);
int main()
{
int N, c, i, count = 0;
while (cin >> N >> c&&N)
{
count++;
for (i = 0; i < N; i++)
cin >> stu[i].number >> stu[i].name >> stu[i].mark;;
switch (c){
case 1:sort(stu,stu+N,cmp1); break;
case 2:sort(stu,stu+N,cmp2); break;
case 3:sort(stu,stu+N,cmp3); break;
}
cout << "Case " << count << ":" << endl;
for (i = 0; i < N; i++)
cout << stu[i].number << ' ' << stu[i].name << ' ' << stu[i].mark << endl;
}
return 0;
}
bool cmp1(student x, student y)
{
if (strcmp(x.number, y.number) < 0)//学号递增排序
return true;
else
return false;
}
bool cmp2(student x, student y)
{
if (strcmp(x.name, y.name)<0)
return true;
else if (strcmp(x.name, y.name) == 0)
{
if (strcmp(x.number, y.number) < 0)     //姓名非递减排序
return true;
else
return false;
}
else
return false;
}
bool cmp3(student x, student y)
{
if (x.mark < y.mark)
return true;
else if (x.mark == y.mark)
{
if (strcmp(x.number, y.number) < 0)        //分数非递减排序
return true;
else
return false;
}
else
return false;
}

转载于:https://www.cnblogs.com/td15980891505/p/4411600.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值