用c++编写以下程序:任意读入三个整数,然后按从小到大的顺序输出这三个数
时间: 2023-04-08 07:00:34 浏览: 150
以下是程序代码:
```c
#include <stdio.h>
int main() {
int a, b, c;
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;
}
```
这个程序可以读入三个整数,然后按从小到大的顺序输出这三个数。
相关问题
如何用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;
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;
}
```
如果您有任何问题或需要进一步的解释,请随时问我。