将三个数从大到小输出:
方法1:创建临时变量
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a = 0, b = 0, c = 0;
int tmp = 0;
scanf_s("%d%d%d", &a, &b, &c);
if (a < b)
{
tmp = a;
a = b;
b = tmp;
}
if (a < c)
{
tmp = a;
a = c;
c = tmp;
}
if (b < c)
{
tmp = b;
b = c;
c = tmp;
}
printf("%d %d %d\n", a, b, c);
system("pause");
return 0;
}
方法2:用函数实现
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
int main()
{
int a = 0, b = 0, c = 0;
int tmp = 0;
scanf_s("%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;
}
时间: 2024-10-14 08:57:12