본문 바로가기

백준 문제풀기/JAVA

[백준 2752 JAVA 자바] 세수정렬

 

3개의 숫자를 오름차순으로 출력합시다

 

평소라면 그냥 Arrays.sort()를 했겠지만

 

딱 3개니깐

완전 기초처럼 오름차순 정렬해봅시다

 

코드입니다

import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		
		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		
		if (a<b) {
			if(c<a) {
				System.out.print(c+" "+a+" "+b);
			}else if (b<c) {
				System.out.print(a+" "+b+" "+c);
			}else {
				System.out.print(a+" "+c+" "+b);
			}
		}else {
			if(c<b) {
				System.out.print(c+" "+b+" "+a);
			}else if (a<c) {
				System.out.print(b+" "+a+" "+c);
			}else {
				System.out.print(b+" "+c+" "+a);
			}
		}
	}
}

어우 코드가 길죠?

 

삼항 연산자를쓰면 훨씬 짧아질거같네요