C语言,给出四个数,从小到大排序
时间: 2023-05-17 17:04:45 浏览: 98
可以使用冒泡排序算法来实现从小到大排序。以下是示例代码:
```
#include <stdio.h>
void bubble_sort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[4];
int i;
printf("请输入四个数:\n");
for (i = 0; i < 4; i++) {
scanf("%d", &arr[i]);
}
bubble_sort(arr, 4);
printf("从小到大排序后的结果为:\n");
for (i = 0; i < 4; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
输入四个数后,程序会使用冒泡排序算法对它们进行排序,并输出从小到大排序后的结果。
相关问题
c语言输入四个数从小到大输出
好的,以下是用C语言编写的实现输入四个数从小到大输出的程序:
```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;
}
```
这个程序中,我们使用了冒泡排序的思想,将输入的四个数从小到大排序,并输出结果。
输入四个整数,从小到大排序的C语言
程序怎么写?
以下是满足条件的C语言程序:
```c
#include <stdio.h>
int main()
{
int nums[4], temp, i, j;
/* 输入四个整数 */
printf("请输入四个整数:\n");
for(i = 0; i < 4; i++)
scanf("%d", &nums[i]);
/* 从小到大排序 */
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3 - i; j++)
{
if(nums[j] > nums[j + 1])
{
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
/* 输出排序结果 */
printf("从小到大排序的结果为:");
for(i = 0; i < 4; i++)
printf("%d ", nums[i]);
printf("\n");
return 0;
}
```
希望能够帮到您!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)