• Home

My Codegate

  • Home

백준 우유 축제 자바 문제풀이

2023/11/29 Posted by Codegate Java No Comments

문제 링크

다음 문제는 처음 입력으로 값이 얼마나 들어올 지 알려준 후 그 다음 입력으로 값이 들어오게 되는데 0 > 1 > 2 > 0 순서로 몇 번이나 진행됐는지 횟수를 출력해주면 되는데

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class MilkFestival {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int count = Integer.parseInt(br.readLine());
        int incre = 0;
        int current = 0;
        int drink = 0;
        int[] walkOrder = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        while(incre<count) {
            if(drink == 0) {
                if(walkOrder[incre] == 0) {
                    current = 0;
                    drink++;
                }
            } else {
                if (current == 0 && walkOrder[incre] == 1) {
                    current = 1;
                    drink++;
                } else if (current == 1 && walkOrder[incre] == 2) {
                    current = 2;
                    drink++;
                } else if (current == 2 && walkOrder[incre] == 0) {
                    current = 0;
                    drink++;
                }
            }
            incre++;
        }
        System.out.println(drink);
    }
}

풀이는 처음 입력받은 횟수만큼 0부터 시작해서 while문을 돌려주고 두 번째로 입력받은 값은 Split으로 나눠준 뒤 int 타입 배열에 넣어주면 되는데 Stream을 사용하지 않고 for를 사용해도 별 차이는 없다.

최초 값의 경우에는 이전 값이 없었기 때문에 int 배열의 현재 값이 0이면 숫자를 추가해주고 이후부터는 0 > 1 > 2 > 0 순서에 맞춰서 맞을 경우 숫자를 올려준 후 마지막에 숫자를 출력해주면 끝이다.

No Comments
0

Leave a Reply Cancel Reply

Introduction

My Codegate

Latest Posts

  • Google Search Console API 연동방법
  • 인텔리제이 Gradle Dependency 최신 버전 보는 방법
  • Wallet-Tracker 개발일지
  • Moralis API 자바로 호출방법
  • IntelliJ Commit 후 Push 따로 하는 방법

Categories

  • My Project (4)
  • Java (42)
  • Algorithm (161)
    • Java (152)
    • Algorithm Knowledge (3)
    • Algorithm site usage (6)
  • Vue.js (1)
  • Spring (4)
  • Docker (2)
  • IntelliJ (20)
  • Uncategorized (7)

Recent Comments

  • Codegate on Hello world!
  • A WordPress Commenter on Hello world!

© 2025 — mycodegate.com

Prev Next