三个数按大小排序用c++编写
时间: 2023-06-01 08:04:11 浏览: 88
以下是使用 C 语言编写的三个数按大小排序程序:
```
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个整数:\n");
scanf("%d%d%d", &a, &b, &c);
if (a > b) {
int temp = a;
a = b;
b = temp;
}
if (a > c) {
int temp = a;
a = c;
c = temp;
}
if (b > c) {
int temp = b;
b = c;
c = temp;
}
printf("按从小到大的顺序排序后的结果为:%d %d %d\n", a, b, c);
return 0;
}
```
运行结果示例:
```
请输入三个整数:
12
-5
8
按从小到大的顺序排序后的结果为:-5 8 12
```
相关问题
如何用c++程序编写将三个数按从小到大的顺序排序
可以使用冒泡排序法对三个数进行排序,具体步骤如下:
1. 定义三个变量 a、b、c,分别存储三个数的值。
2. 从小到大依次比较 a、b、c 三个数的大小,如果 a > b 则交换 a 和 b 的值,如果 b > c 则交换 b 和 c 的值。
3. 经过一次完整的比较,最大的数就被排到了最后面,因此只需要再进行两次比较即可。
下面是一个示例代码:
```c++
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
if (a > b) {
int temp = a;
a = b;
b = temp;
}
if (b > c) {
int temp = b;
b = c;
c = temp;
}
if (a > b) {
int temp = a;
a = b;
b = temp;
}
cout << a << " " << b << " " << c << endl;
return 0;
}
```
这个程序从标准输入读入三个整数,然后使用冒泡排序法将它们按从小到大的顺序输出到标准输出。
用c++代码写一个给三个数排序的程序
好的,以下是一个用 C 语言编写的给三个数排序的程序:
```c
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个整数:\n");
scanf("%d%d%d", &a, &b, &c);
if (a > b) {
int temp = a;
a = b;
b = temp;
}
if (a > c) {
int temp = a;
a = c;
c = temp;
}
if (b > c) {
int temp = b;
b = c;
c = temp;
}
printf("排序后的结果为:%d %d %d\n", a, b, c);
return ;
}
```
希望能够帮到你!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)