Codeup exercises students sort

 

Title description

Excel can sort a group of records by any specified column. Now please write a program to achieve similar functions.

For each test case, first output 1 line of "Case i:", where i is the number of the test case (starting from 1). Then output the results sorted according to the requirements in N rows, that is: when C=1, sort by student ID in ascending order; when C=2, sort by name in non-decreasing dictionary order; when C=3, sort by grade The non-decreasing sort. When several students have the same name or the same grades, they are sorted in ascending order by their student number.

enter

The test input contains several test cases. The first row of each test case contains two integers N (N<=100000) and C, where N is the number of records, and C is the column number for the specified sort. There are N lines below, each line contains a student record. Each student record consists of student ID (6 digits, no duplicate student ID in the same group of tests), name (a string of no more than 8 digits and no spaces), and grade (an integer in the closed interval [0, 100] ), each item is separated by a space. When N=0 is read, all input ends, and the corresponding result is not output.

Output

For each test case, first output 1 line of "Case i:", where i is the number of the test case (starting from 1). Then output the results sorted according to the requirements in N rows, that is: when C=1, sort by student ID in ascending order; when C=2, sort by name in non-decreasing dictionary order; when C=3, sort by grade The non-decreasing sort. When several students have the same name or the same grades, they are sorted in ascending order by their student number.

Sample input Copy

4 1 
000001 Zhao 75 
000004 Qian 88 
000003 Li 64 
000002 Sun 90 
4 2 
000005 Zhao 95 
000011 
Zhao 75 000007 Qian 68 000006 
Sun 85 
4 3 
000002 Qian 88 
000015 Li 95 
000012 Zhao 70 
000009 Sun 95 
0 3

Sample output Copy

Case 1: 
000001 Zhao 75 
000002 Sun 90 
000003 Li 64 
000004 Qian 88 
Case 2: 
000007 Qian 68 000006 
Sun 85 
000005 Zhao 95 
000011 Zhao 75 
Case 3: 
000012 Zhao 70 
000002 Qian 88 
000009 Sun 95 
000015 Li 95

 

AC code

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>

using namespace std;

const int N = 100010;
int n,c;

struct Stu{
    int id;
    char name[20];
    int score;
}stu[N];

bool cmp1(Stu a,Stu b)
{
    return a.id < b.id;
}

bool cmp2(Stu a,Stu b)
{
    //若名字相同,按学号递增排序
    //先用t保存比较的结果,直接调用字符数组的名字,即if (a.name == b.name) 是不行的
    int t = strcmp(a.name,b.name);
    if (t == 0) return a.id < b.id;
    else return t < 0;
}

bool cmp3(Stu a,Stu b)
{
    //若分数相同,按学号递增排序
    if (a.score == b.score) return a.id < b.id;
    else return a.score < b.score;
}

int main()
{
    //注意case_num不能在循环内声明,否则每次输出都是1
    int case_num = 1;
    while (scanf("%d %d",&n,&c) != EOF)
    {
        if (n == 0) break;
        for (int i = 0;i < n;i++)
            scanf("%d%s%d",&stu[i].id,&stu[i].name,&stu[i].score);
        if (c == 1) sort(stu,stu + n,cmp1);
        if (c == 2) sort(stu,stu + n,cmp2);
        if (c == 3) sort(stu,stu + n,cmp3);
        printf("Case %d:\n",case_num);
        for (int i = 0;i < n;i++)
        {
            //%06d表示一共六位,不足则前面补零,最后要注意本题使用cin/cout会TLE
            printf("%06d %s %d\n",stu[i].id,stu[i].name,stu[i].score);
        }
        case_num += 1;
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/smallrain6/article/details/107263968