编程 将用户输入的三个数 按从小到大顺序输出
时间: 2023-05-22 15:04:34 浏览: 41
非常好的问题!可以使用以下代码实现:
```
#include <iostream>
using namespace std;
int main() {
int num1, num2, num3;
cout << "请输入三个整数: ";
cin >> num1 >> num2 >> num3;
int arr[3] = {num1, num2, num3};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2 - i; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
cout << "按从小到大的顺序输出: " << arr[0] << ", " << arr[1] << ", " << arr[2] << endl;
return 0;
}
```
这段代码中,我们使用了冒泡排序的方法,将三个数依次比较,按照从小到大的顺序排序并输出。
相关问题
c语言编程输入三个实数要求按从小到大的顺序输出
你可以使用以下的C语言代码实现输入三个实数,并按照从小到大的顺序输出:
```c
#include <stdio.h>
int main()
{
float a, b, c;
printf("请输入三个实数:\n");
scanf("%f %f %f", &a, &b, &c);
if (a > b) {
float temp = a;
a = b;
b = temp;
}
if (b > c) {
float temp = b;
b = c;
c = temp;
}
if (a > b) {
float temp = a;
a = b;
b = temp;
}
printf("从小到大的顺序输出:%f %f %f\n", a, b, c);
return 0;
}
```
上述代码中,我们使用了三个if语句和一个临时变量temp来确定三个实数的大小关系,从而实现了按照从小到大的顺序输出。
编程输入三个整数,按从小到大的顺序输出
可以使用以下代码实现:
```
a = int(input("请输入第一个整数:"))
b = int(input("请输入第二个整数:"))
c = int(input("请输入第三个整数:"))
if a <= b and a <= c:
if b <= c:
print(a, b, c)
else:
print(a, c, b)
elif b <= a and b <= c:
if a <= c:
print(b, a, c)
else:
print(b, c, a)
else:
if a <= b:
print(c, a, b)
else:
print(c, b, a)
```
这段代码首先通过 `input()` 函数获取用户输入的三个整数,然后使用 `if` 语句进行比较,最终输出按从小到大排序后的结果。