输入的10个数由大到小排序c语言
时间: 2024-05-05 19:21:40 浏览: 18
以下是使用冒泡排序法对10个数进行从大到小排序的C语言代码:
```c
#include <stdio.h>
int main() {
int arr[10] = {5, 8, 2, 10, 3, 1, 7, 9, 6, 4};
int temp;
// 冒泡排序
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9 - i; j++) {
if (arr[j] < arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// 输出排序后的数组
printf("排序后的数组:");
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
输出结果为:
```
排序后的数组:10 9 8 7 6 5 4 3 2 1
```
相关问题
C语言输入三个数由大到小排序输出
好的,以下是C语言代码实现:
```c
#include <stdio.h>
int main()
{
int a, b, c, temp;
printf("请输入三个整数:");
scanf("%d %d %d", &a, &b, &c);
if (a < b) {
temp = a;
a = b;
b = temp;
}
if (a < c) {
temp = a;
a = c;
c = temp;
}
if (b < c) {
temp = b;
b = c;
c = temp;
}
printf("从大到小排序为:%d %d %d\n", a, b, c);
return 0;
}
```
程序中,先输入三个整数,然后使用 if 语句和中间变量进行比较和交换,最终输出排序后的结果。
输入二十个整数并且从大到小排序c语言
以下是输入20个整数并从大到小排序的C语言代码:
```c
#include <stdio.h>
int main() {
int arr[20];
int i, j, temp;
// 输入20个整数
printf("请输入20个整数:\n");
for (i = 0; i < 20; i++) {
scanf("%d", &arr[i]);
}
// 选择排序,大到小排序
for (i = 0; i < 19; i++) {
for (j = i + 1; j < 20; j++) {
if (arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
// 输出排序后的结果
printf("从大到小排序后的结果为:\n");
for (i = 0; i < 20; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)