풀이 방법
책이 무조건 차례대로 들어가기 때문에 if문을 사용해서 차례대로 비교해주면서 넣어주면 된다.
N이 0일땐 StringTokenizer를 생성하면 nullpointer 오류가 나기 때문에 그 점만 주의하면 된다.
내 코드
package com.company;
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
if (N != 0)
st = new StringTokenizer(br.readLine());
int cnt = 0;
int weight = 0;
for (int i = 0; i < N; i++) {
int x = Integer.parseInt(st.nextToken());
if (weight == 0 && x <= M)
cnt++;
if (weight + x <= M) {
weight += x;
} else if (x <= M) {
cnt++;
weight = x;
}
weight %= M;
}
System.out.println(cnt);
}
}
'코딩테스트 > [백준] 코딩테스트 연습' 카테고리의 다른 글
빙산 - 2573번 (0) | 2021.10.07 |
---|---|
크게 만들기 - 2812번 (0) | 2021.10.07 |
돌다리 건너기 - 2602번 (0) | 2021.10.06 |
괄호 제거 - 2800번 (0) | 2021.10.06 |
듣보잡 - 1764번 (0) | 2021.10.06 |