Follows 331.c (Total 58 lines):

/* @JUDGE_ID:4461XX 331 C */
/* A */
#include<stdio.h>
#define N 5

int arr[N] , n ;
void input( void )
{
	int i ;

	for( i=0 ; i<n ; i++ )
		scanf( "%d" , &arr[i] ) ;
}
int sorted( void )
{
	int i ;

	for( i=0 ; i<n-1 ; i++ )
		if( arr[i]>arr[i+1] ) return 0 ;
	
	return 1 ;
}
int recursive( void )
{
	int i , tmp , time=0 ;

	if( sorted() ) return 1 ;
	else
		for( i=0 ; i<n-1 ; i++ )
			if( arr[i]>arr[i+1] ){
				/* swap( arr[i] , arr[i+1] ) */
				tmp = arr[i] ;
				arr[i] = arr[i+1] ;
				arr[i+1] = tmp ;
				time += recursive() ;
				/* swap( arr[i] , arr[i+1] ) */
				tmp = arr[i] ;
				arr[i] = arr[i+1] ;
				arr[i+1] = tmp ;
			}
	
	return time ;
}
int main( void )
{
	int time ;

	for( time=1 ; ; time++ ){
		scanf( "%d" , &n ) ;
		if( !n ) break ;
		
		input() ;
		printf( "There are %d swap maps for input data set %d.\n" , sorted() ? 0 : recursive() , time ) ;
	}

	return 0 ;
}
/* @END_OF_SOURCE_CODE */

Back to statistics
Ya-Lin Huang (C)
huangyl@gmail.com