题目描述
Excel can sort records according to any column. Now you are supposed to imitate this function.
输入描述:
Each input file contains one test case. For each case, the first line contains two integers N (<=100000) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, each contains a record of a student. A student’s record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).
输出描述:
For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID’s; if C = 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades. If there are several students who have the same name or grade, they must be sorted according to their ID’s in increasing order.
输入例子:
3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
输出例子:
000001 Zoe 60
000007 James 85
000010 Amy 90
题目分析及实现方法
原本的想法是自己根据所给的列实现排序,用了选择排序的方法,但是其算法复杂度较高,导致三个用例运行超时,查询资料后发现可以调用"#include《algorithm》"中的sort函数,只需要自己写好比较函数即可快速的排序。下次遇到排序的题,首先要想到sort函数,而不是自己去实现排序算法。
代码实现(选择排序超时版本)
#include <iostream>
#include <algorithm>
using namespace std;
struct Student{
string id;
string name;
int grade;
};
int main()
{
int num ,col;
cin >>num >>col;
Student stu[num];
for(int i = 0; i<num ; i++){
string id,name;
int grade;
cin >> stu[i].id >> stu[i].name>>stu[i].grade;
}
if(col == 1){
for(int i =0; i< num;i++){
string min = stu[i].id;
int temp = 0;
for(int j =i+1;j<num;j++){
if(stu[j].id < min){
min = stu[j].id;
temp = j;
}
}
if(i<temp)
swap(stu[i],stu[temp]);
}
}else if(col ==2){
for(int i =0; i< num;i++){
string min = stu[i].name;
string minID = stu[i].id;
int temp = 0;
for(int j =i+1;j<num;j++){
if(stu[j].name <= min){
if(stu[j].name == min){
if(stu[j].id < minID){
minID = stu[j].id;
min = stu[j].name;
temp = j;
}
}else{
minID = stu[j].id;
min = stu[j].name;
temp = j;
}
}
}
if(i<temp)
swap(stu[i],stu[temp]);
}
}else if(col == 3){
for(int i =0; i< num;i++){
int min = stu[i].grade;
string minID = stu[i].id;
int temp = 0;
for(int j =i+1;j<num;j++){
if(stu[j].grade <= min){
if(stu[j].grade == min){
if(stu[j].id < minID){
minID = stu[j].id;
min = stu[j].grade;
temp = j;
}
}else{
minID = stu[j].id;
min = stu[j].grade;
temp = j;
}
}
}
if(i<temp)
swap(stu[i],stu[temp]);
}
}
for(int i =0 ;i<num;i++){
cout<<stu[i].id<<" "<<stu[i].name<<" "<<stu[i].grade<<endl;
}
return 0;
}
正确解法(C++实现版本)
#include <iostream>
#include <algorithm>
using namespace std;
int num ,col;
struct Student{
string id;
string name;
int grade;
};
bool cmp1(Student a,Student b) {
if(col==1)
return a.id < b.id;
else if(col==2){
if(a.name == b.name)
return a.id < b.id;
else
return a.name<b.name;
}
else{
if(a.grade==b.grade)
return a.id<b.id;
else
return a.grade<b.grade;
}
}
int main()
{
cin >>num >>col;
Student stu[num];
for(int i = 0; i<num ; i++){
string id,name;
int grade;
cin >> stu[i].id >> stu[i].name>>stu[i].grade;
}
sort(stu,stu+num,cmp1);
for(int i =0 ;i<num;i++){
cout<<stu[i].id<<" "<<stu[i].name<<" "<<stu[i].grade<<endl;
}
return 0;
}