Blue Bridge cup of java sorting algorithm training

Sorting algorithm training

Resource constraints

Time limit: 1.0s Memory Limit: 512.0MB

Problem Description

Write a program, input three integers, then the program will be arranged according to the three largest to smallest integer.
  Input format: input single line, i.e. three integers, separated by a space.
  Output formats: Output only one line, that is the result of the sort.
  Sample input and output

Sample input

9 2 30

Sample Output

30 9 2

import java.util.Scanner;
 
public class Main {
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in  = new Scanner(System.in);
		int[] x = new int[3];
		int temp, i, j;
		for (i = 0; i < 3; i++) {
			x[i] = in.nextInt();
		}
		for (i = 0; i < 3; i++) {
			for (j = i; j < 3; j++) {
				if (x[j] > x[i]) {
					temp = x[i];
					x[i] = x[j];
					x[j] = temp;
				}
			}
			System.out.print(x[i] + " ");
		}
	}
 
}
Published 24 original articles · won praise 2 · Views 164

Guess you like

Origin blog.csdn.net/weixin_44570019/article/details/104515844