题目:输入十个整数,将其中最小的数与第一个数交换,将最大的数与最后一个数对调
# include <stdio.h> void swap ( int * a, int * b) { int temp = * a; * a = * b; * b = temp;
} int main ( ) { int numbers[ 10 ] ; int i; int minIndex = 0 , maxIndex = 0 ; printf ( "请输入十个整数:\n" ) ; for ( i = 0 ; i < 10 ; i++ ) { scanf ( "%d" , & numbers[ i] ) ; if ( i == 0 ) { minIndex = maxIndex = i; } else if ( numbers[ i] < numbers[ minIndex] ) { minIndex = i; } else if ( numbers[ i] > numbers[ maxIndex] ) { maxIndex = i; } } if ( minIndex != 0 ) { swap ( & numbers[ 0 ] , & numbers[ minIndex] ) ; } if ( maxIndex != 9 ) { swap ( & numbers[ 9 ] , & numbers[ maxIndex] ) ; } printf ( "调整后的数组为:\n" ) ; for ( i = 0 ; i < 10 ; i++ ) { printf ( "%d " , numbers[ i] ) ; } printf ( "\n" ) ; return 0 ;
}