table of Contents
1 title
Input number 3 a,b,c
, the output order of size.
2 analysis
Sort three numbers, you only need to compare three
a
Andb
comparing, ifa > b
the exchangea
andb
the value ofa
Andc
comparing, ifa > c
the exchangea
andc
the value ofb
Andc
comparing, ifb > c
the exchangeb
andc
the value of
After three comparison, and then output a
, b
, c
is arranged in order
3 Implementation
#include <stdio.h>
int main()
{
int a;
int b;
int c;
int t; // 临时变量用于交换两变量的值
printf("请输入a、b、c的值,中间用空格隔开:");
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;
}
printf("排序后a、b、c的值为%d、%d、%d", a, b, c);
}
4 run results
请输入a、b、c的值,中间用空格隔开:4 1 7
排序后a、b、c的值为1、4、7