1 . 利用If进行排序
#include <stdio.h> // 三个数排序
int main()
{
int a, b, c;
int temp;
printf("请输入三个整数");
scanf_s("%d %d %d", &a,&b,&c);
if (a > b)
{
temp = b;
b = a;
a = temp;
}
if (a > c)
{
temp = c;
c = a;
a = temp;
}
if (b > c)
{
temp = c;
c = b;
b = temp;
}
printf("%d %d %d", a,b,c);
return 0;
}
2.利用数组的冒泡法
#include <stdio.h> // 数组方法 三个整数排序
int main()
{
int a[3];
int temp;
for (int i = 0; i <= 2; ++i)
{
scanf_s("%d", &a[i]);
}
for (int i = 0; i < 2; ++i)
{
for(int j = 0 ; j < 2 - i ; ++j)
if (a[j] > a[j + 1])
{
temp = a[j + 1];
a[j+1] = a[j];
a[j] = temp;
}
}
printf("%d %d %d", a[0], a[1], a[2]);
return 0;
}
数组可以推广到位数更多的整数排序