HDOJ1862 ( EXCEL排序 ) 【排序函数】

 

Problem : 1862 ( EXCEL排序 )     Judge Status : Accepted
RunId : 5941834    Language : C++    Author : qq1203456195
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
 1 #include<stdio.h> 
 2 #include<stdlib.h> 
 3 #include<string.h> 
 4 struct e 
 5 { 
 6     char t[20],name[20]; 
 7     int s; 
 8 }p[100005]; 
 9 int n,c; 
10 int cmp1( const void *a,const void *b ) 
11 { 
12     struct e f1 = *( (e *)a ),f2 = *( ( e * )b ); 
13     return strcmp( f1.t,f2.t ); 
14 } 
15 int cmp2( const void *a,const void *b ) 
16 { 
17     struct e f1 = *( (e *)a ),f2 = *( ( e * )b ); 
18     int t = strcmp( f1.name,f2.name ); 
19     if( t ) 
20         return t; 
21     else
22         return strcmp( f1.t,f2.t ); 
23 } 
24 int cmp3( const void *a,const void *b ) 
25 { 
26     struct e f1 = *( (e *)a ),f2 = *( ( e * )b ); 
27     int t = f1.s - f2.s; 
28     if( t ) 
29         return t; 
30     else
31         return strcmp( f1.t,f2.t ); 
32 } 
33 int main( ) 
34 { 
35     int t = 0; 
36     while( scanf( "%d%d",&n,&c ) ,n|c ) 
37     { 
38         ++t; 
39         for( int i = 0; i < n; ++i ) 
40             scanf( "%s%s%d",p[i].t,p[i].name,&p[i].s ); 
41         if( c == 1 ) 
42             qsort( p,n,sizeof( p[0] ),cmp1 ); 
43         if( c == 2 ) 
44             qsort( p,n,sizeof( p[0] ),cmp2 ); 
45         if( c == 3 ) 
46             qsort( p,n,sizeof( p[0] ),cmp3 ); 
47         printf( "Case %d:\n",t ); 
48         for( int i = 0; i < n; ++i ) 
49             printf( "%s %s %d\n",p[i].t,p[i].name,p[i].s ); 
50     } 
51     return 0; 
52 }

 

posted @ 2012-05-14 13:32  ZH奶酪  阅读(269)  评论(0编辑  收藏  举报