输入10个整数存入数组,将值最大的元素与第一个元素进行值交换,值最小的元素与 最后一个元素进行值交换,然后输出数组。
时间: 2023-06-11 19:05:47 浏览: 36
好的,您的要求已被记录下来。下面是我的回答:
请问您所需的语言类型是什么呢?我这里给您提供一个C语言的实现:
```c
#include <stdio.h>
int main() {
int arr[10], maxIndex = 0, minIndex = 0, tmp;
printf("请输入10个整数:\n");
for(int i = 0; i < 10; ++i) {
scanf("%d", &arr[i]);
if(arr[i] > arr[maxIndex]) maxIndex = i;
if(arr[i] < arr[minIndex]) minIndex = i;
}
tmp = arr[maxIndex];
arr[maxIndex] = arr[0];
arr[0] = tmp;
tmp = arr[minIndex];
arr[minIndex] = arr[9];
arr[9] = tmp;
printf("交换后数组内容为:\n");
for(int i = 0; i < 10; ++i) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
感谢您的提问,希望我的回答能对您有所帮助!
相关问题
输入10个整数存入数组,将值最大的元素与第一个元素进行值交换,值最小的元素与 最后一个元素进行值交换,然后输出数组
好的,我可以回答这个问题。
以下是实现代码:
```python
numbers = list(map(int, input("请输入10个整数,以空格隔开:").split()))
max_index = numbers.index(max(numbers))
min_index = numbers.index(min(numbers))
# 交换值最大的元素和第一个元素
numbers[0], numbers[max_index] = numbers[max_index], numbers[0]
# 如果值最小的元素已经在最后一个位置,则无需交换
if min_index != 9:
# 交换值最小的元素和最后一个元素
numbers[9], numbers[min_index] = numbers[min_index], numbers[9]
print("交换后的数组为:", numbers)
```
输入示例:
```
请输入10个整数,以空格隔开:3 6 1 8 2 9 4 7 5 0
```
输出示例:
```
交换后的数组为: [9, 6, 1, 8, 2, 5, 4, 7, 3, 0]
```
输入10个整数存入数组,将最大的元素与第一个进行交换,最小的元素与最后一个进行交换,然后输出数组
下面是Python的实现代码:
```python
a = [] # 定义一个空列表
for i in range(10):
a.append(int(input("请输入第%d个整数:" % (i+1))))
# 找出最大值和最小值的下标
max_index = 0
min_index = 0
for i in range(1, 10):
if a[i] > a[max_index]:
max_index = i
if a[i] < a[min_index]:
min_index = i
# 交换最大值和第一个元素
a[max_index], a[0] = a[0], a[max_index]
# 如果最小值的下标是0,则要重新找最小值的下标
if min_index == 0:
min_index = a.index(min(a[1:]))
# 交换最小值和最后一个元素
a[min_index], a[-1] = a[-1], a[min_index]
print("交换后的数组为:", a)
```
输出示例:
```
请输入第1个整数:5
请输入第2个整数:10
请输入第3个整数:3
请输入第4个整数:7
请输入第5个整数:9
请输入第6个整数:2
请输入第7个整数:1
请输入第8个整数:8
请输入第9个整数:6
请输入第10个整数:4
交换后的数组为: [1, 10, 3, 7, 9, 2, 5, 8, 6, 4]
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)