Excel can sort records according to any column. Now you are supposed to imitate this function.
Input
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).
Output
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.
Sample Input 13 1 000007 James 85 000010 Amy 90 000001 Zoe 60Sample Output 1
000001 Zoe 60 000007 James 85 000010 Amy 90Sample Input 2
4 2 000007 James 85 000010 Amy 90 000001 Zoe 60 000002 James 98Sample Output 2
000010 Amy 90 000002 James 98 000007 James 85 000001 Zoe 60Sample Input 3
4 3 000007 James 85 000010 Amy 90 000001 Zoe 60 000002 James 90Sample Output 3
000001 Zoe 60 000007 James 85 000002 James 90000010 Amy 90
#include<iostream> #include<vector> #include<string.h> #include<algorithm> #include<stdio.h> using namespace std; struct student{ int ID; char name[9]; int grade; }; bool cmp1(const student& stu1,const student& stu2){ return stu1.ID < stu2.ID; } bool cmp2(const student& stu1,const student& stu2){ if(strcmp(stu1.name,stu2.name) != 0){ if(strcmp(stu1.name,stu2.name) < 0){ return true; }else{ return false; } }else{ return stu1.ID < stu2.ID; } } bool cmp3(const student& stu1,const student& stu2){ if(stu1.grade != stu2.grade){ return stu1.grade < stu2.grade; }else{ return stu1.ID < stu2.ID; } } int main(){ for(int n,c;scanf("%d%d",&n,&c)!=EOF;){ vector<student>students; for(int i = 0;i < n;i++){ student temp; scanf("%d%s%d",&temp.ID,temp.name,&temp.grade); students.push_back(temp); } if(c == 1){ sort(students.begin(),students.end(),cmp1); }else if(c == 2){ sort(students.begin(),students.end(),cmp2); }else if(c == 3){ sort(students.begin(),students.end(),cmp3); } for(int i = 0;i < students.size();i++){ printf("%06d %s %d\n",students[i].ID,students[i].name,students[i].grade); } } return 0; } /*需要用scanf cin会超时*/
本文介绍了一个模拟Excel排序功能的程序设计案例。该程序可以根据指定的列(如ID、姓名或成绩)对学生的记录进行排序,并提供了多个样例输入输出用于验证程序的正确性。
199

被折叠的 条评论
为什么被折叠?



