输入在一行中给出3个整数,其间以空格分隔 输出格式: 在一行中将3个整数从小到大输出,其间以“->”相连。 输入样例: 4 2 8 输出样例 2->4->8 代码长度限制 16
时间: 2024-03-14 17:43:24 浏览: 48
我明白了,这是一道简单的排序问题。可以使用列表存储输入的三个数,然后使用 sort() 方法对列表进行排序,最后将排序后的三个数连接成一个字符串输出就可以了。下面是代码实现:
```python
nums = input().split()
nums = [int(num) for num in nums]
nums.sort()
result = '->'.join([str(num) for num in nums])
print(result)
```
输入样例:4 2 8
输出样例:2->4->8
相关问题
题要求将输入的任意3个整数从小到大输出。 输入格式: 输入在一行中给出3个整数,其间以空格分隔。 输出格式: 在一行中将3个整数从小到大输出,其间以“->”相连
这道题目要求将输入的任意3个整数从小到大输出,可以使用冒泡排序的方法来实现。具体思路是:先将输入的三个整数存储到数组中,然后使用冒泡排序将它们从小到大排序,最后按照要求输出即可。
以下是代码实现:
```
#include <iostream>
using namespace std;
int main() {
int a[3];
for (int i = 0; i < 3; i++) {
cin >> a[i];
}
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 3 - 1 - j; i++) {
if (a[i] > a[i + 1]) {
// 交换两个数
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
for (int i = 0; i < 3; i++) {
if (i == 2) {
cout << a[i];
} else {
cout << a[i] << "->";
}
}
return 0;
}
```
本题要求将输入的任意 3 个整数从小到大输出。 输入格式: 输入在一行中给出 3 个整数,其间以空格分隔。 输出格式: 在一行中将 3 个整数从小到大输出,其间以“->”相连
这道题目要求将输入的任意3个整数从小到大输出,可以使用选择排序的思想,将三个数进行比较,找到最小值,然后将最小值放在第一个位置,再在剩下的两个数中找到最小值,放在第二个位置,最后剩下的数就是最大值,放在第三个位置即可。以下是三个不同语言的代码实现:
C++代码实现:
```
#include <iostream>
using namespace std;
int main() {
int a[3];
for (int i = 0; i < 3; i++) {
cin >> a[i];
}
for (int i = 0; i < 2; i++) {
for (int j = i + 1; j < 3; j++) {
if (a[i] > a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
cout << a[0] << "->" << a[1] << "->" << a[2]; return 0;
}
```
Python代码实现:
```
a = list(map(int, input().split()))
a.sort()
print(str(a[0]) + "->" + str(a[1]) + "->" + str(a[2]))
```
Java代码实现:
```
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] a = new int[3];
for (int i = 0; i < 3; i++) {
a[i] = sc.nextInt();
}
Arrays.sort(a);
System.out.println(a[0] + "->" + a[1] + "->" + a[2]);
}
}
```
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)