编写C语言程序,从键盘输入10个整数,利用选择法对其按从大到小进行排序并输出结果。
时间: 2024-05-24 17:13:33 浏览: 54
可以的,以下是实现该程序的C语言代码:
#include <stdio.h>
int main()
{
int i, j, k, temp;
int a[10];
printf("请输入10个整数:\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<9;i++)
{
k=i;
for(j=i+1;j<10;j++)
{
if(a[j]>a[k])
{
k=j;
}
}
if(k!=i)
{
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
}
printf("按从大到小排序后的结果为:\n");
for(i=0;i<10;i++)
{
printf("%d ",a[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. 最后,我们将输出排序后的数组。
希望这个程序能够帮助到您!
用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;
}
```
希望能帮到你。
相关推荐
![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)