PAT 1028. List Sorting (25)

Source: Internet
Author: User
Tags strcmp

1028. List Sorting (25)

Excel can sort records according to any column. Now is supposed to imitate this function.

Input

Each input file contains the one test case. For each case, the first line contains-integers N (<=100000) and C, where N is the number of records and C are the C Olumn that is 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 hers distinct ID (a 6-digit number), name (a string with no more than 8 characters WI Thout space), and grade (an integer between 0 and inclusive).

Output

For each test case, the output of the sorting result in N lines. That's, if C = 1 then the records must being sorted in increasing order according to ID ' s; If C = 2 Then the records must is sorted in non-decreasing order according to names; And if C = 3 then the records must is sorted in non-decreasing order according to grades. If There is several students who has the same name or grade, they must is sorted according to their ID ' s in increasing o Rder.

Sample Input 1
3 1000007 James 85000010 Amy 90000001 Zoe 60
Sample Output 1
000001 Zoe 60000007 James 85000010 Amy 90
Sample Input 2
4 2000007 James 85000010 Amy 90000001 Zoe 60000002 James 98
Sample Output 2
000010 Amy 90000002 James 98000007 James 85000001 Zoe 60
Sample Input 3
4 3000007 James 85000010 Amy 90000001 Zoe 60000002 James 90
Sample Output 3
000001 Zoe 60000007 James 85000002 James 90000010 Amy 90


has just begun to write in C + +, using string and vector, and using CIN and cout for input and output, found the last test point run timeout.
It was originally thought that there was a point in the topic to sort by fractions, because the fractional sort could be sorted by bucket (buck-sort), and then improved the algorithm for sorting by fractions, and the result found that the last point was still timed out.
It was later discovered that the problem with CIN and cout would take a lot of time to change the original string to char, and input and output with scanf and printf, and the last point could pass through.
1#include <iostream>2#include <cstring>3#include <algorithm>4 5 using namespacestd;6 7 classStudent8 {9 Private:Ten     Charid[7]; One     Charname[9]; A     intgrade; -  Public: -FriendBOOLcmpConststudent& S1,Conststudent&S2); the     voidRead () {scanf ("%s%s%d", ID, name, &grade); } -     voidOutput () {printf ("%s%s%d", ID, name, grade); } -     intGetgrade () {returngrade;} - }; +  - intcolumn; +Student students[100001]; A  at BOOLcmpConststudent& S1,Conststudent&S2) - { -     if(Column = =1) -         returnstrcmp (S1.id, s2.id) <0; -     Else if(Column = =2) -     { in         if(strcmp (S1.name, s2.name)) -             returnstrcmp (S1.name, S2.name) <0; to         Else +             returnstrcmp (S1.id, s2.id) <0; -     } the     Else *     { $         if(S1.grade! =S2.grade)Panax Notoginseng             returnS1.grade <S2.grade; -         Else  the             returnstrcmp (S1.id, s2.id) <0; +     } A } the  + intMain () - { $     intRecordnum; $scanf"%d%d", &recordnum, &column); -  -      for(inti =0; i < Recordnum; i++) the     { - Student Stu;Wuyi Stu.read (); theStudents[i] =Stu; -     } Wu      -Sort (students, students +Recordnum, CMP); About      for(inti =0; i < Recordnum; i++) $     { - students[i].output (); -         if(I < recordnum-1) -printf"\ n"); A     } +}

PAT 1028. List Sorting (25)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.