输入4个整数,要求从大到小排列
时间: 2023-05-26 16:02:42 浏览: 37
思路:可以使用冒泡排序、选择排序、插入排序等算法进行排序。
以下是冒泡排序的实现:
```python
nums = list(map(int, input().split()))
n = len(nums)
for i in range(n):
for j in range(n-i-1):
if nums[j] < nums[j+1]:
nums[j], nums[j+1] = nums[j+1], nums[j]
print(nums)
```
输入示例:
```
5 2 7 1
```
输出示例:
```
[7, 5, 2, 1]
```
相关问题
输入四个整数从大到小排列
可以使用冒泡排序或者快速排序等算法进行排序。以下是一个快速排序的示例代码:
```
#include <iostream>
using namespace std;
void quick_sort(int arr[], int left, int right) {
if (left >= right) {
return;
}
int pivot = arr[right];
int i = left - 1;
for (int j = left; j < right; j++) {
if (arr[j] >= pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[right]);
quick_sort(arr, left, i);
quick_sort(arr, i + 2, right);
}
int main() {
int arr[4];
cout << "请输入四个整数:";
for (int i = 0; i < 4; i++) {
cin >> arr[i];
}
quick_sort(arr, 0, 3);
cout << "排序后的结果为:";
for (int i = 0; i < 4; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
```
输入四个整数并执行程序,将会输出排序后的结果。
例如输入:4 2 5 1
排序后的结果为:5 4 2 1
c++输入四个整数从大到小排列的代码
以下是一个简单的 C 代码,可以输入四个整数并将它们从大到小排列:
```
#include <stdio.h>
int main() {
int a, b, c, d;
printf("请输入四个整数:\n");
scanf("%d%d%d%d", &a, &b, &c, &d);
if (a < b) {
int temp = a;
a = b;
b = temp;
}
if (a < c) {
int temp = a;
a = c;
c = temp;
}
if (a < d) {
int temp = a;
a = d;
d = temp;
}
if (b < c) {
int temp = b;
b = c;
c = temp;
}
if (b < d) {
int temp = b;
b = d;
d = temp;
}
if (c < d) {
int temp = c;
c = d;
d = temp;
}
printf("从大到小排列的结果为:%d %d %d %d\n", a, b, c, d);
return 0;
}
```
这个程序使用了多个 if 语句来比较四个整数的大小,并将它们按照从大到小的顺序排列。
相关推荐
![](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)