c语言4个数字大小排序
时间: 2024-03-05 21:53:29 浏览: 80
以下是使用C语言实现四个数字大小排序的示例代码:
#include <stdio.h>
int main() {
int a, b, c, d, temp;
printf("请输入四个整数:\n");
scanf("%d%d%d%d", &a, &b, &c, &d);
if (a > b) {
temp = a;
a = b;
b = temp;
}
if (a > c) {
temp = a;
a = c;
c = temp;
}
if (a > d) {
temp = a;
a = d;
d = temp;
}
if (b > c) {
temp = b;
b = c;
c = temp;
}
if (b > d) {
temp = b;
b = d;
d = temp;
}
if (c > d) {
temp = c;
c = d;
d = temp;
}
printf("排序后的结果为:%d %d %d %d\n", a, b, c, d);
return 0;
}
首先,我们通过 scanf
函数从用户输入中获取四个整数。然后,我们使用多个 if
语句来比较四个数字的大小,并将它们按升序排列。最后,我们使用 printf
函数输出排序后的结果。
相关推荐


















