迴圈比較
#include<stdio.h> //挑選法
#define N 10
void main() { int a[N],m,n,i;
for ( i=0;i<N;i++ ) scanf("%d",&a[i]);
m=n=a[0]; for ( i=1;i<N;i++ ) if ( m<a[i] ) m=a[i]; else if ( n>a[i] ) n=a[i];
printf("最大值%d,最小值%d\n",m,n);
}
#include<stdio.h> //排序法
void main() {
int a[N],i,j,k;
for ( i=0;i<N-1;i++ ) for ( j=i+1;j<N;j++ ) if ( a[i]<a[j] ) {k=a[i];a[i]=a[j];a[j]=k;}
prinntf("最大值%d,最小值%d\n",a[0],a[N-1]);
迴圈比較
#include<stdio.h> //挑選法
#define N 10
void main() { int a[N],m,n,i;
for ( i=0;i<N;i++ ) scanf("%d",&a[i]);
m=n=a[0]; for ( i=1;i<N;i++ ) if ( m<a[i] ) m=a[i]; else if ( n>a[i] ) n=a[i];
printf("最大值%d,最小值%d\n",m,n);
}
#include<stdio.h> //排序法
#define N 10
void main() {
int a[N],i,j,k;
for ( i=0;i<N;i++ ) scanf("%d",&a[i]);
for ( i=0;i<N-1;i++ ) for ( j=i+1;j<N;j++ ) if ( a[i]<a[j] ) {k=a[i];a[i]=a[j];a[j]=k;}
prinntf("最大值%d,最小值%d\n",a[0],a[N-1]);
}
}