指针:调用自定义交换函数,完成三个数整从小到大排列

Description
调用自定义交换函数swap(int *p1, int *p2),完成三个整数从小到大排列
Input
多组测试数据,每组输入三个任意整数
Output
输出从小到大排列的三个数
Sample Input
9 2 7
0 -2 12
8 3 1
Sample Output
2 7 9
-2 0 12
1 3 8

#include<stdio.h>
int swap(intp1,intp2)
{
int t;
if(*p1>*p2)
{
t=*p1;//*p1即p1代表的地址所指的变量,在这里是int型,即a
*p1=*p2;
*p2=t;
}
}
int main()
{
int a,b,c;
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
swap(&a,&b);//三个式子顺序很重要,swap(a,b)或swap(*p,*q)都是错的
swap(&a,&c);
swap(&b,&c);
printf("%d %d %d\n",a,b,c);
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/z2431435/article/details/84697467
今日推荐