Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
oh, yeah!一次就ac!我觉得这种题就该用快排。 如果有什么可以改进的还请不吝赐教,谢谢! #include <stdio.h> #include <stdlib.h> typedef struct node{ char *s; int count; } Node; int get_count( char* result, int length ){ int i, j, count; count = 0; for( i = 0; i < length; i++ ){ for( j = i + 1; j < length; j++ ){ if( *( result + i ) > *( result + j ) ){ count++; } } } return count; } int cmp( const void* x, const void* y ){ Node *x1, *y1; x1 = ( Node* )x; y1 = ( Node* )y; return x1->count - y1->count; } int main( ){ int l, n; int i; Node *result; char *input; scanf( "%d%d", &l, &n ); result = ( Node* )malloc( sizeof( Node ) * n ); for( i = 0; i < n; i++ ){ input = ( char * )malloc ( sizeof( char ) * ( l + 1 ) ); scanf( "%s", input ); result[i].s = input; result[i].count = get_count( input, l ); /* printf( "%d\n", result[i].count );*/ } qsort( result, n, sizeof( Node ), cmp ); for( i = 0; i < n; i++ ){ printf( "%s\n", result[i].s ); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator