输入4个整数,从小到大排序,c语言
时间: 2024-05-08 21:15:08 浏览: 13
实现如下:
```c
#include <stdio.h>
int main()
{
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
int temp;
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", a, b, c, d);
return 0;
}
```
首先从输入中读取4个整数,然后利用冒泡排序法,将它们从小到大排序。最后输出排序后的结果。
相关问题
输入4个整数,从小到大排序c语言
示例代码:
```c
#include <stdio.h>
int main() {
int a, b, c, d;
printf("请输入4个整数:");
scanf("%d%d%d%d", &a, &b, &c, &d);
// 冒泡排序
int arr[4] = {a, b, c, d};
int i, j, temp;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3 - i; j++) {
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("排序后的结果为:%d %d %d %d\n", arr[0], arr[1], arr[2], arr[3]);
return 0;
}
```
运行结果:
```
请输入4个整数:5 2 9 1
排序后的结果为:1 2 5 9
```
输入4个整数,从小到大排序c语言代码
下面是一种简单的C语言代码实现:
```
#include <stdio.h>
int main() {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
int temp;
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;
}
```
首先,定义4个整数变量a、b、c、d,并通过scanf()函数从标准输入中读入4个整数。
接着,使用冒泡排序的思想,依次比较相邻的两个数,将大的数往后移动。具体实现过程中,使用了6个if语句和一个temp变量来完成排序。
最后,使用printf()函数将排序后的结果输出到标准输出中。