[JAVA] Baekjoon - 1547 공

hyng·2022년 6월 8일
0

Baekjoon

목록 보기
1/5

컵의 위치를 바꿀 때마다 공의 위치를 수정한다.

import java.util.*;

class Main {
    private static int ballLoc = 1;
    public static void main(String[]args) {
        Scanner sc = new Scanner(System.in);

        int M = sc.nextInt();
        for (int i = 0; i < M; i++) {
            int cup1 = sc.nextInt();
            int cup2 = sc.nextInt();

            change(cup1, cup2);
        }
        System.out.println(ballLoc);
    }

    private static void change(int cup1, int cup2) {
        if (cup1 == ballLoc) {
            ballLoc = cup2;
            return;
        }

        if (cup2 == ballLoc) {
            ballLoc = cup1;
        }
    }
}

profile
공부하고 알게 된 내용을 기록하는 블로그

0개의 댓글