1028. List Sorting (25)

题目如下:

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 1
3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
Sample Output 1
000001 Zoe 60
000007 James 85
000010 Amy 90
Sample Input 2
4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98
Sample Output 2
000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60
Sample Input 3
4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90
Sample Output 3
000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90

题目本身很简单,只需要设计结构体Person存储一条记录,然后使用容器容纳所有Person记录,使用sort函数按照不同的规则排序即可。

最初我最后一个测试点出现了超时,当时十分费解,还认为可能是STL的排序函数速度不够快,后来在网上查阅大家对这个题的体会发现是cin和cout不够快造成的,记得姥姥曾经说过少用cin和cout,但是没有重视T T,果然将cin和cout改成scanf和printf即可通过最后一个测试点。

需要注意的是,使用C++编程常常会忽略stdio.h这个头文件,而scanf和printf都是在这个头文件下的,严格的来说,应该包含此头文件(虽然不包含PAT的编译器不会报错)。

使用scanf比较蛋疼的地方在于存储姓名就只能使用char数组了,而且比较还要使用strcmp,注意输入%s到char数组时,不需要取地址,因为数组名本身就是地址。

为了方便起见,应该使用int存储ID,输出时使用%06d来保证正确的前导0个数。

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

struct Person{

    int ID;
    char name[20];
    int score;

};

int compare1(Person a, Person b){
    return a.ID < b.ID;
}

int compare2(Person a, Person b){
    if(strcmp(a.name,b.name) < 0){
        return true;
    }else if(strcmp(a.name,b.name)==0 && a.ID < b.ID){
        return true;
    }
    return false;
}

int compare3(Person a, Person b){

    if(a.score < b.score){
        return true;
    }else if(a.score == b.score && a.ID < b.ID){
        return true;
    }
    return false;

}

int main()
{
    int N,C;
    cin >> N >> C;
    vector persons(N);
    int id;
    string name;
    int score;
    for(int i = 0; i < N; i++){
        scanf("%d%s%d",&(persons[i].ID),persons[i].name,&(persons[i].score));
    }
    switch(C){
    case 1:
        sort(persons.begin(),persons.end(),compare1);
        break;
    case 2:
        sort(persons.begin(),persons.end(),compare2);
        break;
    case 3:
        sort(persons.begin(),persons.end(),compare3);
        break;
    }
    for(int i = 0; i < persons.size(); i++){
        id = persons[i].ID;
        score = persons[i].score;
        printf("%06d %s %d\n",id,persons[i].name,score);

    }

    return 0;
}


转载于:https://www.cnblogs.com/aiwz/p/6154164.html

你可能感兴趣的:(c/c++)