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);
}
}
}
}
어우 코드가 길죠?
삼항 연산자를쓰면 훨씬 짧아질거같네요
'백준 문제풀기 > JAVA' 카테고리의 다른 글
[백준 2798 JAVA 자바] 블랙잭 (0) | 2023.08.08 |
---|---|
[백준 2753 JAVA 자바] 윤년 (0) | 2023.08.08 |
[백준 2751 JAVA 자바] 수 정렬하기 2 (0) | 2023.08.08 |
[백준 2750 JAVA 자바] 수 정렬하기 (0) | 2023.08.08 |
[백준 2747 JAVA 자바] 피보나치 수 (0) | 2023.08.08 |