标签:将三个数排序
将三个数从大到小输出:
方法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;
}
本文出自 “Stand out or Get out” 博客,转载请与作者联系!
标签:将三个数排序
原文地址:http://jiazhenzhen.blog.51cto.com/10781724/1709770