用c语言编写程序,从键盘输入10个整数,利用选择法对其按从大到小进行排序并输出结果。
时间: 2024-05-14 07:13:29 浏览: 26
好的,这是一个关于C语言编程的问题。以下是解答:
```
#include <stdio.h>
int main() {
int arr[10], i, j, temp;
printf("请输入10个整数:");
for (i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < 9; i++) {
for (j = i+1; j < 10; j++) {
if (arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("从大到小排序后的结果为:");
for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
希望这个答案对您有帮助!接下来,您有什么其他问题吗?
相关问题
用c语言编写程序,从键盘输入10个整数,利用选择法对其按从大到小进行排序并输出结果
好的,这个问题可以回答。以下是 C 语言程序的代码:
```
#include <stdio.h>
int main() {
int numbers[10];
printf("请输入10个整数:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &numbers[i]);
}
// 选择法排序
for (int i = 0; i < 9; i++) {
int minIndex = i;
for (int j = i + 1; j < 10; j++) {
if (numbers[j] < numbers[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
int temp = numbers[i];
numbers[i] = numbers[minIndex];
numbers[minIndex] = temp;
}
}
printf("排序后的结果为:\n");
for (int i = 0; i < 10; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
```
希望能帮到你。
c语言编写程序,从键盘输入10个整数,利用选择法对其按从大到小进行排序并输出结果。
好的,以下是您所需要的C语言程序代码:
```c
#include <stdio.h>
void selection_sort(int arr[], int n) {
int i, j, max_idx, temp;
for (i = 0; i < n - 1; i++) {
max_idx = i;
for (j = i + 1; j < n; j++) {
if (arr[j] > arr[max_idx]) {
max_idx = j;
}
}
// Swap the maximum element with the current element
temp = arr[i];
arr[i] = arr[max_idx];
arr[max_idx] = temp;
}
}
int main() {
int arr[10];
int i;
printf("Enter 10 integers:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
selection_sort(arr, 10);
printf("Sorted array in descending order:\n");
for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
代码解释:
1. 我们首先定义了一个名为 `selection_sort` 的函数,它将使用选择排序算法对数组进行排序。
2. 在 `main` 函数中,我们首先定义了一个包含 10 个整数的数组,并从键盘输入这些整数。
3. 接下来,我们将调用 `selection_sort` 函数来对数组进行排序。
4. 最后,我们将输出排序后的数组。
希望这个程序能够帮助到您!
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.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)