(1)用冒泡法将从控制台输入的十个学生的成绩从高到低进行排序
时间: 2024-02-15 20:01:38 浏览: 30
好的,这是一个简单的冒泡排序的示例代码,它可以将从控制台输入的十个学生的成绩从高到低进行排序:
```python
# 定义一个列表用于存储十个学生的成绩
scores = []
# 从控制台读取十个学生的成绩
for i in range(10):
score = int(input("请输入第%d个学生的成绩:" % (i+1)))
scores.append(score)
# 冒泡排序
n = len(scores)
for i in range(n):
for j in range(0, n-i-1):
if scores[j] < scores[j+1]:
scores[j], scores[j+1] = scores[j+1], scores[j]
# 输出排序后的结果
print("排序后的结果为:")
for score in scores:
print(score)
```
在这个代码中,我们首先定义了一个列表 `scores` 用于存储十个学生的成绩。然后通过循环读取十个学生的成绩,将它们存储到 `scores` 列表中。接着,我们使用冒泡排序算法对 `scores` 列表进行排序。最后,我们输出排序后的结果。
相关问题
java从控制台输入十个数,对十个数进行数字冒泡排序
可以使用Java中的Scanner类来从控制台读取输入的十个数,然后使用冒泡排序算法对这些数进行排序。代码如下:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] arr = new int[10];
Scanner scanner = new Scanner(System.in);
System.out.println("请输入十个整数:");
for (int i = 0; i < 10; i++) {
arr[i] = scanner.nextInt();
}
// 冒泡排序
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println("排序后的结果为:");
for (int num : arr) {
System.out.print(num + " ");
}
}
}
```
运行程序后,控制台会提示输入十个整数,输入完毕后会输出排好序的结果。
使用java语言从控制台输入一系列数据,用冒泡法排序。
可以使用Java代码实现冒泡排序,首先从控制台读入一系列数据,然后对这些数据进行排序:
```java
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一系列整数,用空格分隔:");
String input = scanner.nextLine();
String[] numbers = input.split(" ");
int[] array = new int[numbers.length];
for (int i = 0; i < numbers.length; i++) {
array[i] = Integer.parseInt(numbers[i]);
}
bubbleSort(array);
System.out.print("排序后的结果为:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
public static void bubbleSort(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
}
```
上面的代码中,首先从控制台读入一系列整数,然后使用冒泡排序对这些整数进行排序。排序后,输出结果到控制台。可以通过以下方式运行程序:
```
请输入一系列整数,用空格分隔:3 1 4 1 5 9 2 6 5 3
排序后的结果为:1 1 2 3 3 4 5 5 6 9
```
可以看到,程序成功对输入的整数进行了排序。