#include
#include
int print( int*, int );
int swap( int&, int& );
int main()
{
int *a, n, i;
scanf( "%d", &n );
a = ( int* ) malloc ( ( n + 1 ) * sizeof( int ) );
for( i = 1; i <= n; i++ )
a[i] = i;
for( i = 1; i <= n; i++ )
swap( a[i], a[rand()%n+1] );
print( a, n );
for( i = 1; i <= n; i++ )
{
while( a[i] != i )
swap( a[i], a[a[i]] );
}
print( a, n );
return 0;
}
int swap( int &i, int &j )
{
int temp = i;
i = j;
j = temp;
return 0;
}
int print( int a[], int n )
{
int i;
for( i = 1; i <= n; i++ )
printf( "%4d", a[i] );
printf( " " );
return 0;
}
注意! 数组中的数和下标的对应关系!!!
int i,b;
for i =0;i
|