输入10个整数,用程序用c语言编程将其中最大数与最小数位置互换,,然后输出互换后的数组
时间: 2024-03-13 22:43:35 浏览: 66
好的,以下是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
```
阅读全文
相关推荐
![doc](https://img-home.csdnimg.cn/images/20241231044833.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)
![c](https://img-home.csdnimg.cn/images/20250102104920.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)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)