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.
3 1 000007 James 85 000010 Amy 90 000001 Zoe 60
000001 Zoe 60 000007 James 85 000010 Amy 90Sample Input 2
4 2 000007 James 85 000010 Amy 90 000001 Zoe 60 000002 James 98Sample Output 2
000010 Amy 90 000002 James 98 000007 James 85 000001 Zoe 60Sample Input 3
4 3 000007 James 85 000010 Amy 90 000001 Zoe 60 000002 James 90Sample Output 3
000001 Zoe 60 000007 James 85 000002 James 90 000010 Amy 90
#include <cstdio>
#include <string.h>
#include <vector>
#include <algorithm>
#define MAX 100000
using namespace std ;
struct stuNode
{
char ID [10];
char name[10] ;
int score ;
stuNode () {}
stuNode ( char *id , char *na , int s)
{
strcpy( ID , id ) ;
strcpy( name , na ) ;
score = s ;
}
} list[MAX];
int cmp1(const void * a , const void * b )
{
const stuNode * p1 = ( const stuNode*) a ;
const stuNode * p2 = (const stuNode * ) b ;
if( strcmp( p1->ID , p2->ID ) > 0 )
return 1 ;
else
return 0 ;
}
int cmp2 ( const void *a , const void * b )
{
const stuNode *p1 = (const stuNode * ) a ;
const stuNode *p2 = ( const stuNode * ) b ;
if ( strcmp(p1->name , p2->name ) != 0 )
return strcmp( p1->name , p2->name) ;
else
{
if ( strcmp(p1->ID , p2->ID) > 0)
return 1 ;
else
return 0 ;
}
}
int cmp3 ( const void *a , const void *b )
{
const stuNode * p1 = (const stuNode *) a ;
const stuNode *p2 = ( const stuNode * ) b ;
if ( p2->score != p1->score )
{
if ( p2->score > p1->score )
return 0 ;
else if ( p2->score < p1->score )
return 1 ;
}
else
{
if ( strcmp(p1->ID , p2->ID) > 0 )
return 1 ;
else
return 0 ;
}
}
void printList ( int num )
{
for ( int i = 0 ; i < num ; i++ )
{
printf("%s %s %d", list[i].ID , list[i].name , list[i].score ) ;
if ( i != num -1 )
printf("\n") ;
}
}
int main ( void )
{
int score ,stuNum , caseNum;
char name[10] , ID[10] ;
scanf("%d%d", &stuNum , &caseNum) ;
for (int i = 0 ; i < stuNum ; i++ )
{
scanf("%s" , ID ) ;
scanf("%s" , name ) ;
scanf("%d", &score ) ;
list[i]=stuNode(ID , name , score) ;
}
switch ( caseNum )
{
case 1:
qsort( list, stuNum , sizeof( stuNode ) , cmp1) ;
break ;
case 2:
qsort( list , stuNum , sizeof( stuNode ) ,cmp2 ) ;
break ;
case 3:
qsort( list, stuNum , sizeof( stuNode ) , cmp3) ;
break;
default :
break ;
}
printList ( stuNum ) ;
return 0 ;
}
做这道题的时候,突然想到了以前没有使用 stl ,直接敲排序代码的岁月,不知道学会使用 stl 之后,是一种进步,还是一种对原来算法实现的一种退步。
这道题就是根据不同的字段进行排序, 只要使用 qsort 之后,根据每次传入排序字段的数目进行选择分支即可。
不过需要注意的就是,对 name 和 grade 进行排序的时候要注意的就是,在 name ,grade 相同的时候需要根据 ID 的递增来进行排序。
我希望我下次实现代码的时候,可以使输入、输出以及中间处理环节的函数划分更加的明确。