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
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
class Student
{
public:
string m_number;
string m_name;
int m_score;
Student(const string &number = "",const string &name = "",int score = -1)
:m_number(number),m_name(name),m_score(score){}
public:
void print() const
{
cout << m_number << " " << m_name << " " << m_score << endl;
}
};
class NumberSort
{
public:
bool operator()(const Student &lhs, const Student &rhs) const
{
return lhs.m_number < rhs.m_number;
}
};
class NameSort
{
public:
bool operator()(const Student &lhs, const Student &rhs) const
{
if(lhs.m_name != rhs.m_name)
return lhs.m_name < rhs.m_name;
else
return lhs.m_number < rhs.m_number;
}
};
class ScoreSort
{
public:
bool operator()(const Student &lhs, const Student &rhs) const
{
if(lhs.m_score != rhs.m_score)
return lhs.m_score < rhs.m_score;
else
return lhs.m_number < rhs.m_number;
}
};
void print(const Student &stu)
{
cout << stu.m_name << " " << stu.m_number << " " << stu.m_score << endl;
}
int main()
{
typedef vector<Student> StuVec;
StuVec stu;
int count = 0;
int N,C;
while(cin >> N >> C)
{
if(N == 0 && C == 0)
break;
for(int i = 0; i != N; ++i)
{
string number;
string name;
int score;
cin >> number >> name >> score;
stu.push_back(Student(number,name,score));
}
switch(C)
{
case 1:
sort(stu.begin(),stu.end(),NumberSort());
break;
case 2:
sort(stu.begin(),stu.end(),NameSort());
break;
case 3:
sort(stu.begin(),stu.end(),ScoreSort());
break;
}
cout << "Case " << ++count << ":" << endl;
for_each(stu.begin(),stu.end(),mem_fun_ref(&Student::print));
stu.clear();
}
return 0;
}