输入10个整数,用程序编程将其中最大数与最小数位置互换
时间: 2024-03-13 17:43:31 浏览: 19
好的,以下是Python的代码实现:
```python
numbers = input("请输入10个整数(用空格分隔):").split()
numbers = [int(num) for num in numbers] # 将输入的字符串转换为整数列表
max_index = numbers.index(max(numbers)) # 找到最大数的索引
min_index = numbers.index(min(numbers)) # 找到最小数的索引
# 交换最大数和最小数的位置
numbers[max_index], numbers[min_index] = numbers[min_index], numbers[max_index]
print("交换后的列表:", numbers)
```
输入示例:
```
请输入10个整数(用空格分隔):1 2 3 4 5 6 7 8 9 10
```
输出示例:
```
交换后的列表: [10, 2, 3, 4, 5, 6, 7, 8, 9, 1]
```
相关问题
输入10个整数,用程序用c语言编程将其中最大数与最小数位置互换,,然后输出互换后的数组
好的,以下是C语言的代码实现:
```c
#include <stdio.h>
int main() {
int numbers[10];
int max_index = 0, min_index = 0;
printf("请输入10个整数(用空格分隔):");
for (int i = 0; i < 10; i++) {
scanf("%d", &numbers[i]);
}
// 找到最大数和最小数的索引
for (int i = 1; i < 10; i++) {
if (numbers[i] > numbers[max_index]) {
max_index = i;
}
if (numbers[i] < numbers[min_index]) {
min_index = i;
}
}
// 交换最大数和最小数的位置
int temp = numbers[max_index];
numbers[max_index] = numbers[min_index];
numbers[min_index] = temp;
printf("交换后的数组:");
for (int i = 0; i < 10; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
```
输入示例:
```
请输入10个整数(用空格分隔):1 2 3 4 5 6 7 8 9 10
```
输出示例:
```
交换后的数组:10 2 3 4 5 6 7 8 9 1
```
输入10个整数,用函数编程将其中最大数与最小数位置互换,然后输出互换后的数组
以下是Python代码实现:
def swap_max_min(arr):
max_index = arr.index(max(arr))
min_index = arr.index(min(arr))
arr[max_index], arr[min_index] = arr[min_index], arr[max_index]
return arr
nums = []
for i in range(10):
num = int(input("请输入第{}个整数:".format(i+1)))
nums.append(num)
print("原始数组:", nums)
swapped_nums = swap_max_min(nums)
print("互换后的数组:", swapped_nums)