解答:
#include <stdio.h>
#include <string.h>
void input(int* a) {
for(int i=0; i<=9; i++)
scanf("%d", a+i);
}
void process(int* a) {
int max_location = 9;
int min_location = 0;
//将最大值移到最后
//找出最大值
for(int i=0; i<=8; i++){
if(*(a+i) > *(a+max_location)) {
max_location = i;
}
}
//最大值与最后一个元素交换位置
int temp = *(a+9);
*(a+9) = *(a+max_location);
*(a+max_location) = temp;
//将最小值移到最前
//找出最小值
for(int i=9; i>=1; i--){
if(*(a+i) < *(a+min_location)) {
min_location = i;
}
}
//最小值与第一个元素交换位置
temp = *(a);
*(a) = *(a+min_location);
*(a+min_location) = temp;
}
void print(int* a) {
for(int i=0; i<=9; i++){
if(i<9)
printf("%d ", *(a+i));
else
printf("%d\n", *(a+i));
}
}
int main() {
int a[10];
input(a);
process(a);
print(a);
return 0;
}
总结:
- scanf要取地址,printf不用
- 指针变量+常数表示地址