#include
int main()
{
int test[]={15,5,1,6,0,8,12,7,2,9};
int temp,i,j;
int max=0,min=0;
int n=sizeof(test)sizeof(test[0]);
for(i=1;i
if(test[i]>test[max]) max=i;
if(test[i]
test[0]=test[min];
test[min]=temp;
temp=test[n-1];
test[n-1]=test[max];
test[max]=temp;
for(j=0;j
printf("%d\t",test[j]);
}
}
供参考:
#include <stdio.h>
int main()
{
int test[]={15,5,1,6,0,8,12,7,2,9};
int temp,i,j;
int max=0,min=0;
int n=sizeof(test)/sizeof(test[0]);
for(i=1;i<n;i++)
{
if(test[i]>test[max]) max=i;
if(test[i]<test[min]) min=i;
}
temp=test[0];
test[0]=test[min];
test[min]=temp;
if (max == 0)//如果数组的第一个值就是最大值,
max = min;//那么刚才在交换的过程中,已经被移动了位置
temp=test[n-1];
test[n-1]=test[max];
test[max]=temp;
for(j=0;j<n;j++)
{
printf("%d\t",test[j]);
}
return 0;
}