본문 바로가기

Algorithm/백준

[백준] 6840: Who is in the middle? - JAVA [자바]

 


 

문제


 

  1. 입력 하는 세 값중 중간 값을 출력하는 문제입니다.

 

문제 풀이


 

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int number1 = sc.nextInt();
        int number2 = sc.nextInt();
        int number3 = sc.nextInt();

        ArrayList<Integer> al = new ArrayList<>();
        al.add(number1);
        al.add(number2);
        al.add(number3);
        //(1)
        Collections.sort(al);
		//(2)
        System.out.println(al.get(1));
        //(3)
    }
}

 

  1. Scanner를 이용해 입력한 세 수를 ArrayList인 al에 저장합니다.
  2. al을 정렬합니다.
  3. al에서 두번째 인덱스의 값을 출력합니다.

 

결과