输入四个整数从小到大排列的C语言代码
时间: 2023-05-13 15:07:27 浏览: 59
#include <stdio.h>
int main() {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
int arr[4] = {a, b, c, d};
int temp;
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 4; j++) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for (int i = 0; i < 4; i++) {
printf("%d ", arr[i]);
}
return 0;
}
相关问题
用C语言编写,使用指针编写一个程序,使得输入四个整数,从小到大排列输出
好的,让我来回答您的问题。
以下是使用指针和C语言编写的程序,可以输入四个整数并按从小到大的顺序输出它们:
```c
#include <stdio.h>
void sort(int *a, int *b, int *c, int *d);
int main() {
int a, b, c, d;
printf("请输入四个整数:\n");
scanf("%d %d %d %d", &a, &b, &c, &d);
sort(&a, &b, &c, &d);
printf("四个整数从小到大排列的结果为:\n");
printf("%d %d %d %d", a, b, c, d);
return 0;
}
void sort(int *a, int *b, int *c, int *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;
}
}
```
希望这个程序可以帮到您!
用c语言别写一个将四个整数进行从小到大的顺序排列程序
### 回答1:
#include <stdio.h>
int main()
{
int a, b, c, d;
printf("输入四个整数:");
scanf("%d %d %d %d", &a, &b, &c, &d);
if (a > b) {
int temp = a;
a = b;
b = temp;
}
if (b > c) {
int temp = b;
b = c;
c = temp;
}
if (c > d) {
int temp = c;
c = d;
d = temp;
}
if (a > b) {
int temp = a;
a = b;
b = temp;
}
if (b > c) {
int temp = b;
b = c;
c = temp;
}
printf("从小到大排列顺序为:%d %d %d %d\n", a, b, c, d);
return 0;
}
### 回答2:
使用C语言编写一个将四个整数进行从小到大顺序排列的程序可以使用冒泡排序算法。
首先,定义一个数组来存储这四个整数,数组长度为4。然后,使用嵌套的循环进行比较和交换操作,以实现排序目的。
以下是该程序的具体实现:
```c
#include <stdio.h>
void bubbleSort(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]) {
// 交换arr[j]和arr[j+1]
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int arr[4] = {4, 2, 1, 3}; // 要排序的四个整数
int i;
printf("排序前的数组:");
for (i = 0; i < 4; i++) {
printf("%d ", arr[i]);
}
bubbleSort(arr, 4);
printf("\n排序后的数组:");
for (i = 0; i < 4; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
在程序中,`bubbleSort`函数实现了冒泡排序算法。`main`函数定义了要排序的四个整数,并通过调用`bubbleSort`函数进行排序。最后,通过循环打印数组中的元素,展示排序前后的结果。
以上程序的输出结果将为:
```
排序前的数组:4 2 1 3
排序后的数组:1 2 3 4
```
这样,四个整数就按照从小到大的顺序排列完成了。
### 回答3:
下面是一个用C语言编写的将四个整数进行从小到大顺序排列的程序:
```c
#include <stdio.h>
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
void sort(int arr[]) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4 - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
}
}
}
}
int main() {
int arr[4];
printf("请输入四个整数:\n");
for (int i = 0; i < 4; i++) {
scanf("%d", &arr[i]);
}
sort(arr);
printf("排序结果为:\n");
for (int i = 0; i < 4; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
这个程序首先定义了一个用于交换两个整数的辅助函数`swap`。然后定义了一个`sort`函数,利用冒泡排序算法对数组进行排序。`sort`函数内部有两个嵌套循环,每次比较相邻的两个元素,如果顺序相反,则交换它们的位置。最后,`main`函数通过调用`sort`函数对输入的四个整数进行排序,并输出结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)