//第一次尝试:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void compare(int x, int y, int z){
if (x > y){
if (y > z){
printf("%d %d %d", x, y, z);
}
else if (x < z){
printf("%d %d %d", z, x, y);
}
else{
printf("%d %d %d", x, z, y);
}
}
else if (x < y){
if (y < z){
printf("%d %d %d", z, y, x);
}
else if (x > z){
printf("%d %d %d", y, x, z);
}
else{
printf("%d %d %d", y, z, x);
}
}
}
int main()
{
int a = 0;
int b = 0;
int c = 0;
printf("请输入三个数\n");
scanf("%d %d %d", &a, &b, &c);
compare(a, b, c);
return 0;
}
//虽然运行成功,得到了结果,但是代码太繁琐,不够简洁。
//第二次尝试:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void compare(int a, int b, int c) {
for (int i = 0; i < 3; i++) {
int t = 0;
if (a < b) {
t = a; a = b; b = t;
}
else if (b < c) {
t = b; b = c; c = t;
}
else if (a < c) {
t = a; a = c; c = t;
}
}
printf("%d %d %d\n", a, b, c);
}
int main(){
int x = 0;
int y = 0;
int z = 0;
printf("请输入三个数\n");
scanf("%d %d %d", &x, &y, &z);
compare(x, y, z);
return 0;
}
//做出了一些改进,进行三次循环,然后借助临时变量t进行交换,最终得到从大到小的排序。此代码比第一次的结构严谨,且短小,我认为是比较好的改进。