从键盘上输入三个数,按由小到大排序
时间: 2024-05-14 20:12:22 浏览: 7
以下是Python代码实现:
```
a = int(input("请输入第一个数:"))
b = int(input("请输入第二个数:"))
c = int(input("请输入第三个数:"))
if a > b:
a, b = b, a
if a > c:
a, c = c, a
if b > c:
b, c = c, b
print("由小到大排序的结果为:", a, b, c)
```
输出:
```
请输入第一个数:3
请输入第二个数:1
请输入第三个数:5
由小到大排序的结果为: 1 3 5
```
相关问题
编写一main函数 要求利用指针 实现从键盘输入三个数 由小到大排序
好的,下面是使用指针实现从键盘输入三个数并从小到大排序的main函数示例:
```c++
#include <iostream>
using namespace std;
void sort(int *a, int *b, int *c) {
int t;
if (*a > *b) {
t = *a;
*a = *b;
*b = t;
}
if (*a > *c) {
t = *a;
*a = *c;
*c = t;
}
if (*b > *c) {
t = *b;
*b = *c;
*c = t;
}
}
int main() {
int a, b, c;
cout << "请输入三个数:" << endl;
cin >> a >> b >> c;
sort(&a, &b, &c);
cout << "从小到大排序后的结果为:" << a << " " << b << " " << c << endl;
return 0;
}
```
在上面的代码中,sort函数用于实现三个数从小到大排序,使用指针传入三个数的地址,通过比较和交换来实现排序。在main函数中,先从键盘输入三个数,然后调用sort函数进行排序,最后输出排序后的结果。
java从键盘输入三个数,从小到大排序后输出
以下是Java代码:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入三个数:");
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
// 排序
int tmp;
if (a > b) {
tmp = a;
a = b;
b = tmp;
}
if (a > c) {
tmp = a;
a = c;
c = tmp;
}
if (b > c) {
tmp = b;
b = c;
c = tmp;
}
System.out.println("从小到大排序后的结果为:" + a + " " + b + " " + c);
}
}
```
我们使用Scanner类从键盘读取三个数,并使用if语句对它们进行排序。最后输出结果即可。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)