试题 算法训练 排序
资源限制
时间限制:1.0s 内存限制:512.0MB
问题描述
编写一个程序,输入3个整数,然后程序将对这三个整数按照从大到小进行排列。
输入格式:输入只有一行,即三个整数,中间用空格隔开。
输出格式:输出只有一行,即排序后的结果。
输入输出样例
样例输入
9 2 30
样例输出
30 9 2
Java 代码:
import java.io.*;
public class Main {
public static int[] a = new int[3];
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String[] s = in.readLine().split(" ");
for (int i = 0; i < 3; i++)
a[i] = Integer.parseInt(s[i]);
if (a[0] < a[1]) {
swap(0, 1);
}
if (a[0] < a[2]) {
swap(0, 2);
}
if (a[1] < a[2])
swap(1, 2);
for (int i = 0; i < 3; i++)
System.out.print(a[i] + " ");
}
private static void swap(int i, int j) {
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}