码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1862

时间:2015-04-09 21:36:06      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

//开始把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;
}

hdu1862

标签:

原文地址:http://www.cnblogs.com/td15980891505/p/4411600.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!