B-excel sort (sort+ structure)

Source: Internet
Author: User
Tags strcmp

Excel can sort a set of records by any specified column. You are now asked to write programs to implement similar functions.

The input test inputs contain several test cases. The 1th line of each test case contains two integers n (<=100000) and C, where N is the number of records, and C is the column number that specifies the sort. The following are N
Row, each row contains a student record. Each student record consists of a school number (6 digits, no duplicate number in the same set of tests), a name (a string of no more than 8 digits without a space), a score (an integer in the closed interval [0, 100]), separated by 1 spaces between each item. When the n=0 is read, all input ends and the corresponding result is not output.
Output for each test case, first outputs 1 lines "Case I:", where I is the number of the test cases (starting from 1). Then, in N rows, the output is sorted according to the requirements, i.e. when c=1, the order is incremented by the number of studies, and when c=2, the non-descending dictionary order by name; when C=3
, sort by the non-descending order of the scores. When several students have the same name or the same score, they are incremented by their number.
Sample Input

3 1000007 James 85000010 Amy 90000001 Zoe 604 2000007 James 85000010 Amy 90000001 Zoe 60000002 James 984 3000007 James 850 00010 Amy 90000001 Zoe 60000002 James 900 0

Sample Output

Case 1:000001 Zoe 60000007 James 85000010 Amy 90Case 2:000010 Amy 90000002 James 98000007 James 85000001 Zoe 60Case 3:0000 60000007 Zoe James 85000002 James 90000010 Amy 90

Harvest: Further mastery of the structure of the knowledge, previously felt to understand the knowledge can now read, feel very happy

AC Code

#include <stdio.h>#include<string.h>#include<algorithm>using namespacestd;structnood{Charnum[Ten]; Charname[Ten]; intscore;} student[100005];//number of studies increment sortBOOLCMP1 (Nood A, Nood b) {intT_num =strcmp (A.num, b.num); if(T_num = =-1)        return true; Else        return false;}//name Non-descending dictionary orderingBOOLCMP2 (Nood A, Nood b) {intT_name =strcmp (A.name, b.name); if(T_name = =0)    {        intg =strcmp (A.num, b.num); if(g = =-1)            return true; Else            return false; }    Else if(T_name = =-1)        return true; Else if(T_name = =1)        return false;}//grades Non-descending sortBOOLCmp3 (Nood A, Nood b) {if(A.score = =b.score) {intD =strcmp (A.num, b.num); if(d = =-1)            return true; Else            return false; }    Else        returnA.score <B.score;}intMain () {intN, C; intFlag =0;  while(~SCANF ("%d%d", &n, &c)) {flag++; if(n = =0&& c = =0)             Break; Else        {             for(inti =0; I < n; i++) {scanf ("%s%s%d", Student[i].num, Student[i].name, &Student[i].score); }            if(c = =1) {printf ("Case %d:\n", flag); Sort (student, student+N, CMP1);  for(inti =0; I < n; i++) {printf ("%s%s%d\n", Student[i].num, Student[i].name, Student[i].score); }            }            Else if(c = =2) {printf ("Case %d:\n", flag); Sort (student, student+N, CMP2);  for(inti =0; I < n; i++) {printf ("%s%s%d\n", Student[i].num, Student[i].name, Student[i].score); }            }            Else if(c = =3) {printf ("Case %d:\n", flag); Sort (student, student+N, Cmp3);  for(inti =0; I < n; i++) {printf ("%s%s%d\n", Student[i].num, Student[i].name, Student[i].score); }            }        }    }    return 0;}
View Code

About Dictionary Ordering

For example, there is a random variable x that contains the {1 2 3} three values. Its dictionary sort is {1 2 3} {1 3 2} {2 1 3} {2 3 1} {3 1 2} {3 2 1}

B-excel sort (sort+ structure)

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.