a、b、c三个数字由大到小排列,用C语言实现
时间: 2023-04-08 09:05:22 浏览: 60
我可以回答这个问题。可以使用冒泡排序或选择排序来实现。以下是冒泡排序的示例代码:
```
#include <stdio.h>
int main() {
int a = 3, b = 1, c = 2;
int temp;
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", a, b, c);
return 0;
}
```
输出结果为:3 2 1。
相关问题
用c语言if双分支从大到小排列三个数字大小
可以使用以下代码实现:
```c
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个数字:\n");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && b >= c) {
printf("%d >= %d >= %d\n", a, b, c);
} else if (a >= c && c >= b) {
printf("%d >= %d >= %d\n", a, c, b);
} else if (b >= a && a >= c) {
printf("%d >= %d >= %d\n", b, a, c);
} else if (b >= c && c >= a) {
printf("%d >= %d >= %d\n", b, c, a);
} else if (c >= a && a >= b) {
printf("%d >= %d >= %d\n", c, a, b);
} else {
printf("%d >= %d >= %d\n", c, b, a);
}
return 0;
}
```
这段代码可以让用户输入三个数字,然后使用 if 双分支语句从大到小排列这三个数字的大小,并输出结果。
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语句进行交换实现了从小到大排列。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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_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)