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

题目本身非常easy。仅仅须要设计结构体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 <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>

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<Person> 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;
}


posted on 2017-06-15 15:43  yjbjingcha  阅读(126)  评论(0编辑  收藏  举报

导航