https://www.acmicpc.net/problem/1817
1817번: 짐 챙기는 숌
첫째 줄에 책의 개수 N과 박스에 넣을 수 있는 최대 무게 M이 주어진다. N은 0보다 크거나 같고 50보다 작거나 같은 정수이고, M은 1,000보다 작거나 같은 자연수이다. N이 0보다 큰 경우 둘째 줄에 책
www.acmicpc.net
>풀이
/*
* 백준 1817 짐챙기는 숌
* #실버5
* #그리디
* 23.05.07
*/
public class boj_1817 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
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());
int[] arr = new int[N];
if(N > 0) {
st = new StringTokenizer(br.readLine());
for(int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
}
int cnt = N > 0 ? 1 : 0;
int sum = 0;
// 박스에 순서대로 책을 넣어야하는게 조건이다
for(int x : arr) {
sum += x;
if(sum > M) {
cnt++;
sum = x;
}
}
System.out.println(cnt);
}
}
'PS > BOJ' 카테고리의 다른 글
백준 1206 보물 / JAVA (0) | 2023.05.10 |
---|---|
백준 1996 지뢰찾기 / JAVA (0) | 2023.05.09 |
백준 1676 팩토리얼 0의 개수 / JAVA (0) | 2023.05.03 |
백준 1343 폴리오미노 (0) | 2023.04.20 |
백준 5585 거스름돈 / JAVA (0) | 2023.04.19 |
댓글