[Jobdu OJ] 1023 EXCEL排序

题目描述:
    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 
时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。
样例输入:
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
样例输出:
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

问题解析:这道题就是对一个给定的数据集合进行排序,该数据集合包含三个字段,根据给定的第二个参数,对不同的字段进行排序。首先,如何存储这些数据,可以用一个结构体保存一个记录,然后用一个容器保存一次排序的集合。其次,如何对某个字段进行排序,在标准库中有sort算法,但是,它用的是数据成员的operator<来进行比较的,因此,要实现某个字段的比较,就要另外构造比较函数,这里使用函数对象。

#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;

struct student {
	string id;
	string name;
	unsigned int remark;
};

ostream& operator<<(ostream &out, student stu)
{
	out << stu.id << " " << stu.name << " " << stu.remark;
	return out;
}

struct compare_id {
	bool operator()(student x, student y)
	{
		return x.id < y.id;
	}
};

struct compare_name {
	bool operator()(student x, student y)
	{
		if(x.name == y.name) {
			return x.id < y.id;
		}
		else {
			return x.name < y.name;
		}
	}
};

struct compare_remark {
	bool operator()(student x, student y)
	{
		if(x.remark == y.remark) {
			return x.id < y.id;
		}
		else {
			return x.remark < y.remark;
		}
	}
};

int main()
{
	unsigned long records = 0;
	unsigned int c = 0;
	unsigned int cnt = 0;
	vector<student> svec;
	student stu;
	int index = 0;

    ifstream cin("luo.txt");
	while(cin >> records >> c, records && c) {
		svec.resize(records);
		index = 0;
		while(index < records) {
			cin >> svec[index].id >> svec[index].name >> svec[index].remark;
			++index;
		}
		switch(c) {
			case 1:
			    sort(svec.begin(), svec.end(), compare_id());
			    break;
			case 2:
			    sort(svec.begin(), svec.end(), compare_name());
			    break;
			case 3:
			    sort(svec.begin(), svec.end(), compare_remark());
			    break;
			default:
			    break;
		}
		cout << "Case " << ++cnt << ":" << endl;
		copy(svec.begin(), svec.end(), ostream_iterator<student>(cout, "\n"));
		svec.clear();
	}

	return 0;
}

这里用到了C++中的几点知识:

(1)函数的重载:operator<<;

(2)函数对象;

(3)algorithm中的sort。

注意:这题虽然很简单,但是,要注意的问题也很多:
(1)学号字段虽然可以存储在unsigned int中,但是,如果使用unsigned int会WA;
(2)虽然vector的好处是用户可以随意添加元素,但是这里不能自由地进行push_back,否则会WA,而应该先使用resize将容器的大小变成所需要的大小,然后再往里面放置元素,因此,这里使用new也是很方便的;
(3)输出结果时Case后面那个整数是测试用例的编号,而不是排序方式。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值