Skip to content

L1-010 比较大小

本题要求将输入的任意 3 个整数从小到大输出。

输入格式:

输入在一行中给出 3 个整数,其间以空格分隔。

输出格式:

在一行中将 3 个整数从小到大输出,其间以“->”相连。

输入样例:

tex
4 2 8
4 2 8

输出样例:

tex
2->4->8
2->4->8

Solution:

java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] input = in.readLine().split(" ");
        int a = Integer.parseInt(input[0]);
        int b = Integer.parseInt(input[1]);
        int c = Integer.parseInt(input[2]);
        if (a > b) a = a + b - (b = a);
        if (b > c) b = b + c - (c = b);
        if (a > b) a = a + b - (b = a);
        System.out.printf("%d->%d->%d", a, b, c);
    }
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] input = in.readLine().split(" ");
        int a = Integer.parseInt(input[0]);
        int b = Integer.parseInt(input[1]);
        int c = Integer.parseInt(input[2]);
        if (a > b) a = a + b - (b = a);
        if (b > c) b = b + c - (c = b);
        if (a > b) a = a + b - (b = a);
        System.out.printf("%d->%d->%d", a, b, c);
    }
}

Released under the MIT License.