C程序34 三个数排序

题目:输入 3 个数 a,b,c,按大小顺序输出。

程序分析:利用指针方法。

代码

# include<stdio.h>
void swap(int *, int *);
int main(void)
{
		int a, b, c;
		int *p1, *p2, *p3;
		
		printf("Input a, b ,c:");
		scanf("%d %d %d", &a, &b, &c);
		
		p1 = &a;
		p2 = &b;
		p3 = &c;
		if(a>b)
		swap(p1, p2);
		if(a>c)
		swap(p1, p3);
		if(b>c)
		swap(p2, p3);
		printf("%d %d %d\n", a, b, c);
}
void swap(int *s1, int *s2)
{
	int t;
	t = *s1; *s1 = *s2; *s2 = t;
}

运行结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zm_960810/article/details/86105402
今日推荐