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

Re:AC 了 用选择排序就好了啊

Posted by sunkehappy at 2009-03-03 15:45:32 on Problem 1674
In Reply To:AC 了 用选择排序就好了啊 Posted by:382784268 at 2009-02-23 23:35:30
唉!差一点就超时了。
891ms
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
    int i, j, key, sets, *a, num, step, temp;
    scanf("%d", &sets ) ;
    while( sets-- ){
        scanf("%d", &num ) ;
        a = ( int*)malloc( num*sizeof(int) ) ;
        for( i = 0; i < num; ++i )
        	scanf("%d", &a[i] ) ;
        step = 0 ;
        for( i = 0; i < num; ++i ){
        	key = i ;
        	for( j = i+1; j < num; ++j )
                if( a[key] > a[j] )
                	key = j ;
            if( key != i ){
            	temp = a[key] ;
            	a[key] = a[i] ;
            	a[i] = temp ;
            	++ step ;
            }
        }
        free( a ) ;
        printf("%d\n", step ) ;
    }
    return 0 ;
}

Followed by:

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