输入5个整数,对所有整数进行排序,按照降序输出。 输入格式: 输入5个整数。 输出格式: 按照降序输出5个整数。java
时间: 2024-05-01 10:21:33 浏览: 49
好的,以下是 Java 的代码实现:
```java
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Integer[] nums = new Integer[5];
for (int i = 0; i < 5; i++) {
nums[i] = scanner.nextInt();
}
Arrays.sort(nums, Collections.reverseOrder());
for (int i = 0; i < 5; i++) {
System.out.print(nums[i] + " ");
}
}
}
```
首先使用 `Scanner` 类从控制台输入 5 个整数,并存储到一个 `Integer` 数组中。然后使用 `Arrays.sort()` 方法按照降序排序,最后使用循环输出排序后的结果。由于题目要求输出按照降序排列的整数,因此使用了 `Collections.reverseOrder()` 方法来实现。
相关问题
输入正整数n再输入n个正整数并对这个n个整数按照降序排序
好的,这是一个经典的排序问题,可以使用冒泡排序、插入排序、选择排序等多种算法来解决。这里我给出一种简单的选择排序算法的代码实现,可以按照你的要求对n个整数进行降序排序:
```python
n = int(input("请输入整数个数:"))
nums = []
for i in range(n):
num = int(input("请输入第%d个整数:" % (i + 1)))
nums.append(num)
# 选择排序
for i in range(n - 1):
max_index = i
for j in range(i + 1, n):
if nums[j] > nums[max_index]:
max_index = j
if max_index != i:
nums[i], nums[max_index] = nums[max_index], nums[i]
print("降序排序后的结果为:", nums)
```
这段代码首先读入整数的个数n,然后用一个循环读入n个整数,将它们存储到一个列表nums中。接着使用选择排序算法对这n个整数进行排序,最后输出排序后的结果。
用Python编写一个任意输入三个整数对这个三个整数按降序排序并打印输出的程序
```python
a, b, c = map(int, input("请输入三个整数,以空格分隔:").split())
if a >= b and b >= c:
print(a, b, c)
elif a >= c and c >= b:
print(a, c, b)
elif b >= a and a >= c:
print(b, a, c)
elif b >= c and c >= a:
print(b, c, a)
elif c >= a and a >= b:
print(c, a, b)
else:
print(c, b, a)
```
使用 `map` 函数可同时将多个数据转换为指定类型,并使用 `split` 函数将输入的字符串分割为三个整数。按情况进行不同的处理后输出即可。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)