输入四个整数,按从大到小排序输出 多种方法
时间: 2023-05-23 13:04:34 浏览: 52
方法一:使用临时变量交换
```
a, b, c, d = map(int, input().split())
# 冒泡排序
if a < b:
a, b = b, a
if a < c:
a, c = c, a
if a < d:
a, d = d, a
if b < c:
b, c = c, b
if b < d:
b, d = d, b
if c < d:
c, d = d, c
print(a, b, c, d)
```
方法二:使用列表排序
```
lst = list(map(int, input().split()))
lst.sort(reverse=True)
print(*lst)
```
方法三:使用sort()方法
```
a, b, c, d = map(int, input().split())
lst = [a, b, c, d]
lst.sort(reverse=True)
print(*lst)
```
相关问题
输入4个整数,按从大到小排序输出
好的,我非常乐意帮你解决这个问题。以下是按从大到小排序输出的代码:
```
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;
int arr[] = {a, b, c, d};
sort(arr, arr + 4, greater<int>());
for (int i = 0; i < 4; ++i)
{
cout << arr[i] << " ";
}
return 0;
}
```
当你输入4个整数时,程序将把这4个整数存储在数组中,并使用C++ `sort` 函数对它们进行排序,以便从大到小输出。排序使用 `greater<int>` 比较器来比较整数的大小。最后,程序将循环打印排序后的数组。
希望这次的回答有帮助!
输入三个正整数,按从大到小排序输出
可以使用Python内置的sort()函数来实现排序,然后倒序输出即可:
```python
a, b, c = map(int, input().split())
lst = [a, b, c]
lst.sort()
lst.reverse()
print(lst[0], lst[1], lst[2])
```
也可以使用冒泡排序来实现:
```python
a, b, c = map(int, input().split())
lst = [a, b, c]
for i in range(2):
for j in range(2-i):
if lst[j] < lst[j+1]:
lst[j], lst[j+1] = lst[j+1], lst[j]
print(lst[0], lst[1], lst[2])
```
以上两种方法都可以实现将三个正整数按从大到小排序输出。