Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

oh, yeah!一次就ac!

Posted by wcw at 2008-04-01 23:20:00 on Problem 1007
我觉得这种题就该用快排。
如果有什么可以改进的还请不吝赐教,谢谢!

#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:
  • AC (3) y_x 2008-04-03 20:59:42

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator