键盘输入三个数,编写程序将他们从大到小输出。
时间: 2023-04-10 15:03:15 浏览: 93
可以使用冒泡排序算法来实现这个功能,代码如下:
```
#include <stdio.h>
void bubble_sort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int a[3], i;
printf("请输入三个数:\n");
for (i = 0; i < 3; i++) {
scanf("%d", &a[i]);
}
bubble_sort(a, 3);
printf("从大到小排序后的结果为:\n");
for (i = 0; i < 3; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
```
这个程序可以接受用户输入三个数,然后使用冒泡排序算法将它们从大到小排序,最后输出排序后的结果。
相关问题
用c语言编写程序实现从键盘输入三个数,将他们按从小到大的顺序输出
以下是用 C 语言编写的程序,实现从键盘输入三个数,将它们按从小到大的顺序输出:
```c
#include <stdio.h>
int main() {
int a, b, c, t;
printf("请输入三个数,用空格隔开:");
scanf("%d %d %d", &a, &b, &c);
// 将 a 和 b 按从小到大的顺序排列
if (a > b) {
t = a;
a = b;
b = t;
}
// 将 a 和 c 按从小到大的顺序排列
if (a > c) {
t = a;
a = c;
c = t;
}
// 将 b 和 c 按从小到大的顺序排列
if (b > c) {
t = b;
b = c;
c = t;
}
printf("三个数按从小到大的顺序输出为:%d %d %d\n", a, b, c);
return 0;
}
```
程序执行的结果如下:
```
请输入三个数,用空格隔开:3 2 1
三个数按从小到大的顺序输出为:1 2 3
```
编写程序从键盘输入三个数输出三个数最大值
这个问题可以回答。以下是一个示例程序:
```
#include <stdio.h>
int main() {
int a, b, c, max;
printf("请输入三个数:");
scanf("%d %d %d", &a, &b, &c);
max = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
printf("最大值是:%d\n", max);
return 0;
}
```
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![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)