1028 List Sorting (25 分)

Excel can sort records according to any column. Now you are supposed to imitate this function.
Input Specification:

Each input file contains one test case. For each case, the first line contains two integers N (≤10​5​​) 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 Specification:

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 1:

3 1
000007 James 85
000010 Amy 90
000001 Zoe 60

Same output 2:

000001 Zoe 60
000007 James 85
000010 Amy 90

题意:就是给出多条信息,然后排序,输出。
不多说了,直接给出代码:

#include
#pragma GCC optimize(2)
#define MAXN 100010
#define maxnode 2000005
#define sigma_size 26
#define md 12345678
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;

int n,op;
struct node
{
     
    int num,grade;
    char name[100];
};
node P[MAXN];

bool comp_1(node A,node B)
{
     
    return A.num<B.num;
}

bool comp_2(node A,node B)
{
     
    if(strcmp(A.name,B.name)==0)
        return A.num<B.num;
    return strcmp(A.name,B.name)<0;
}

bool comp_3(node A,node B)
{
     
    if(A.grade==B.grade)
        return A.num<B.num;
    return A.grade<B.grade;
}

int main()
{
     
    scanf("%d %d",&n,&op);
    for(int i=1;i<=n;i++)
    {
     
        scanf("%d %s %d",&P[i].num,&P[i].name,&P[i].grade);
    }
    if(op==1)
    {
     
        sort(P+1,P+1+n,comp_1);
    }
    else if(op==2)
    {
     
        sort(P+1,P+1+n,comp_2);
    }
    else if(op==3)
    {
     
        sort(P+1,P+1+n,comp_3);
    }
    for(int i=1;i<=n;i++)
    {
     
        printf("%06d %s %d\n",P[i].num,P[i].name,P[i].grade);
    }
    return 0;
}

你可能感兴趣的:(甲级PAT,甲级,PTA,1028,List,Sorting,(25,分))