3.输入10个整数,将其中最小的数与第一个数对换,把最大的数与最后一个数对换。 写3个函数完成:①输入10个整数;②交换处理;③输出10个整数。 输入样例: 2 4 6 8 10 1 3 5 7 9 输出样例: 1 4 6 8 9 2 3 5 7 10 输入样例: 1 2 3 4 5 6 7 8 9 10 输出样例: 1 2 3 4 5 6 7 8 9 10 输入样例: 10 9 8 7 6 5 4 3 2 1 输出样例: 1 9 8 7 6 5 4 3 2 10
时间: 2024-01-03 09:03:17 浏览: 21
好的,以下是实现该功能的三个函数的代码:
```python
# 输入10个整数
def input_nums():
nums = list(map(int, input().split()))
return nums
# 交换处理
def swap(nums):
# 找到最小值和最大值的索引
min_idx = nums.index(min(nums))
max_idx = nums.index(max(nums))
# 交换最小值和第一个数的位置
nums[0], nums[min_idx] = nums[min_idx], nums[0]
# 如果最大值是第一个数,则交换后最大值的位置变了,需要重新找到最大值的索引
if max_idx == 0:
max_idx = nums.index(max(nums))
# 交换最大值和最后一个数的位置
nums[-1], nums[max_idx] = nums[max_idx], nums[-1]
return nums
# 输出10个整数
def output_nums(nums):
print(' '.join(map(str, nums)))
```
你可以将这三个函数放在一起,完整的程序如下:
相关问题
输入十个整数将其中最小的数与第一个数对换把最大的数与最后一个数对换写三个函数
好的,我可以帮你写三个函数来实现这个功能。
第一个函数用来输入十个整数:
```python
def input_numbers():
nums = []
for i in range(10):
n = int(input(f"请输入第{i+1}个整数:"))
nums.append(n)
return nums
```
第二个函数用来将最小的数与第一个数对换,将最大的数与最后一个数对换:
```python
def swap_numbers(nums):
min_index = nums.index(min(nums))
max_index = nums.index(max(nums))
nums[0], nums[min_index] = nums[min_index], nums[0]
nums[-1], nums[max_index] = nums[max_index], nums[-1]
return nums
```
第三个函数用来输出结果:
```python
def output_result(nums):
print("交换后的结果为:", nums)
```
这三个函数可以组合在一起使用:
```python
if __name__ == "__main__":
nums = input_numbers()
nums = swap_numbers(nums)
output_result(nums)
```
运行程序后,依次输入十个整数,程序会输出交换后的结果。注意,这里的交换并不是真的交换,而是将最小的数与第一个数对换,将最大的数与最后一个数对换,然后输出结果。
输入十个整数,将其中最小的数与第一个数对换,把最大的数与最后一个数对换
以下是Python代码实现:
```python
nums = list(map(int, input("请输入十个整数,用空格隔开:").split()))
min_index = nums.index(min(nums))
nums[], nums[min_index] = nums[min_index], nums[]
max_index = nums.index(max(nums))
nums[-1], nums[max_index] = nums[max_index], nums[-1]
print("处理后的结果为:", nums)
```
输入十个整数,用空格隔开,例如:
```
请输入十个整数,用空格隔开:5 3 8 2 9 1 7 4 6
```
输出结果为:
```
处理后的结果为: [, 3, 8, 2, 9, 1, 7, 4, 6, 5]
```
其中,最小的数与第一个数5对换,最大的数9与最后一个数对换。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)