P5715_三位数排序(深基3.例8)
Description
给出三个整数 a , b , c ( 0 ⩽ a , b , c ⩽ 100 ) a,b,c(0\leqslant a,b,c \leqslant 100) a,b,c(0⩽a,b,c⩽100),要求把这三位整数从小到大排序。
Input
输入三个数
Output
将这三个数从小到大输出
Sample Input
3 2 1
Sample Output
1 2 3
这题就是通过三个判断语句排序这三个数(也可以用swap(a,b);等其他方法),然后将排序后的三个数输出。下面还介绍了一个无变量交换两个数的值,大家也可以看一下。
#include <cstdio>
using namespace std;
int main(){
int a,b,c,t;
scanf("%d %d %d",&a,&b,&c);
if(a>b) {t=a;a=b;b=t;}
if(a>c) {t=a;a=c;c=t;}
if(b>c) {t=b;b=c;c=t;}
/*
实现无变量交换a,b的方法:
a=b-a;
b=b-a;
a=b+a;
*/
printf("%d %d %d",a,b,c);
return 0;
}