代码一:
初始代码。
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <stdlib.h>
int main()
{
int a, b, c, t;
printf("请输入三个数:\n");
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("%d %d %d\n", a, b, c);
system("pause");
return 0;
}
代码二:
为了使代码更加完善,定义一个Swap函数。
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <stdlib.h>
void Swap(int *px, int *py)
{
int tmp = 0;
tmp = *px;//解引用
*px = *py;
*py = tmp;
}
int main()
{
int a, b, c;
printf("请输入三个数:\n");
scanf("%d%d%d", &a, &b, &c);
if(a<b)
{
Swap(&a, &b);
}
if(a<c)
{
Swap(&a, &c);
}
if(b<c)
{
Swap(&b, &c);
}
printf("%d %d %d\n",a, b, c);
system("pause");
return 0;
}