Issue: Excel can sort a set of records by any specified column. You are now asked to write a program to implement similar functional algorithms.
Input
The test input contains 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, the first output is 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 1
000007 James 85
000010 Amy 90
000001 Zoe 60
4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98
4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90
0 0
Sample Output
Case 1:
000001 Zoe 60
000007 James 85
000010 Amy 90
Case 2:
000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60
Case 3:
000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90
Reply:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 100005
using namespace Std;
struct node
{
Char id[20];
Char name[20];
int fen;
} A[MAXN];
BOOL Cmp1 (node X,node y)
{
if (strcmp (X.id, Y.id) < 0)//study number increment sort
return true;
Else
return false;
}
BOOL CMP2 (node X,node y)
{
if (strcmp (X.name,y.name) < 0)//name Dictionary order non-descending sort
return true;
else if (strcmp (x.name, y.name) = = 0)
{
if (strcmp (X.id, Y.id) < 0)//When several students have the same name, the order is incremented by their number
return true;
}
return false;
}
BOOL Cmp3 (node X,node y)
{
if (X.fen<y.fen) return true;//score non-descending sort
else if (X.fen==y.fen)
{
if (strcmp (X.id, Y.id) < 0)//When a number of students have the same score, the order is incremented by their number
return true;
}
return false;
}
int main ()
{
int n,c,t=0,i;
while (scanf ("%d%d", &n,&c), N)
{
for (i=0; i<n; i++)
scanf ("%s%s%d", A[i].id,a[i].name,&a[i].fen);
Switch (c)
{
Case 1:
Sort (A,A+N,CMP1);
Break
Case 2:
Sort (A,A+N,CMP2);
Break
Case 3:
Sort (A,a+n,cmp3);
Break
}
printf ("Case%d:\n", ++t);
for (i=0; i<n; i++)
printf ("%s%s%d\n", A[i].id, A[i].name, A[i].fen);
}
return 0;
}
Specifying sorting issues