#include <
stdio.h>
int main() {
int a[10],i,t,imin,imax,n = 10;
printf("输入10整数(空格隔开):");
for(i = 0; i < n; ++i)
scanf("%d",&a[i]);
printf("处理前:\n");
for(i = 0; i < n; ++i)
printf("%d ",a[i]);
printf("\n");
imin = imax = 0;
for(i = 1; i < n; ++i) {
if(a[i] > a[imax]) imax = i;
if(a[i] < a[imin]) imin = i;
}
if(imin != 0) {
t = a[0];
a[0] = a[imin];
a[imin] = t;
}
if(imax != n - 1) {
t = a[n - 1];
a[n - 1] = a[imax];
a[imax] =t;
}
printf("处理后:\n");
for(i = 0; i < n; ++i)
printf("%d ",a[i]);
printf("\n");
return 0;
}
追问需要用函数的方法来做,谢谢~
追答#include <stdio.h>
void fun(int a[], int n) {
int i,t,imin,imax;
imin = imax = 0;
for(i = 1; i < n; ++i) {
if(a[i] > a[imax]) imax = i;
if(a[i] < a[imin]) imin = i;
}
if(imin != 0) {
t = a[0];
a[0] = a[imin];
a[imin] = t;
}
if(imax != n - 1) {
t = a[n - 1];
a[n - 1] = a[imax];
a[imax] =t;
}
}
int main() {
int i,a[10],n = 10;
printf("输入10整数(空格隔开):");
for(i = 0; i < n; ++i)
scanf("%d",&a[i]);
printf("处理前:\n");
for(i = 0; i < n; ++i)
printf("%d ",a[i]);
printf("\n");
fun(a,n);
printf("处理后:\n");
for(i = 0; i < n; ++i)
printf("%d ",a[i]);
printf("\n");
return 0;
}