程序分析:
通过创建中间变量,依次作对比,若是前比后小,则进行交换。共需要三次对比。
代码展示:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int a = 10, b = 20, c = 30;
int tem;
if (a < b)
{
tem = a;
a = b;
b = tem;
}
if (a < c)
{
tem = a;
a = c;
c = tem;
}
if (b < c)
{
tem = b;
b = c;
c = tem;
}
printf("%d %d %d\n", a, b, c);
system("pause");
return 0;
}
运行结果:
30 20 10