LeeBlog

导航

HDU 3293 sort

这里最好用qsort排序,而在写cmp函数时要注意,对rank进行排序是从大到小,所以那里不要写反了,还有排序优先级是origin,,rank,字典序,这里别弄错了,我悲剧额,对rank排序一直是错的,最后听小白一说才恍然大悟,在此鸣谢小白,直接上代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct e
{
       char w[25],o[25],l[25];
}p[505];
int n;
int cmp1( char s1[],char s2[] )
{
    if( s1[0] == s2[0] )
        return 0;
    if( s1[0] == 'w' )
        return 1;
    if( (s1[0] == 'g') && (s2[0] == 's') )
        return 1;
    return -1;
}
int cmp( const void *a,const void *b )
{
    struct e f1 = *( ( e * )a ),f2 = *( ( e * )b );
    int t = strcmp( f1.o,f2.o );
    if( t )
        return t;
    else
    {
        t = cmp1( f2.l,f1.l );//因为这里一直wa
        if( t )
            return t;
        else
            return strcmp( f1.w,f2.w );
    }
}
int main( )
{
    int t = 0;
    while( scanf( "%d",&n ) != EOF )
    {
           for( int i = 0; i < n; ++i )
                scanf( "%s%s%s",p[i].w,p[i].o,p[i].l );
           qsort( p,n,sizeof( p[0] ),cmp );
           char str1[25],str2[25];
           strcpy( str1,p[0].o );
           printf( "Case %d\n",++t );
           printf( "%s:\n",str1 );
           for( int i = 0; i < n; ++i )
           {
                strcpy( str2,p[i].o );
                if( strcmp( str1,str2 ) )
                {
                    printf( "%s:\n",str2 );
                    strcpy( str1,str2 );
                }
                printf( "          %s %s\n",p[i].w,p[i].l );
            }
           }
    return 0;
}

posted on 2011-05-03 22:44  LeeBlog  阅读(218)  评论(0编辑  收藏  举报