《算法笔记》codeup_100000571_E

解答:

#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;
}

总结:

  1. scanf要取地址,printf不用
  2. 指针变量+常数表示地址
发布了36 篇原创文章 · 获赞 3 · 访问量 1246

猜你喜欢

转载自blog.csdn.net/Zen_Ivan/article/details/105313924