자라는 개발자/문제풀이

백준 c++ 1817 짐 챙기는 숌

자란다 2022. 8. 23. 18:04
728x90
반응형

1817 짐 챙기는 숌


문제풀이

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void fast_io(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
int num,weight,cnt;
vector<string> v;
int main(void)
{
    fast_io();
    cin >> num >> weight;
    int tmp = weight;
    while(num--)
    {
        int book;
        cin >> book;
        if(tmp-book<0)
        {
            cnt++;
            tmp=weight;
        }
            tmp-=book;
    }
    if(tmp==weight)
        cout << cnt;
    else
        cout << cnt +1;
} 

무게만큼 빼는 작업을 반복한 뒤 마지막에 돌때 무게변경이 있었다면 +1을 해주었다.

728x90
반응형

'자라는 개발자 > 문제풀이' 카테고리의 다른 글

leetcode 242. Valid Anagram  (0) 2022.11.13
백준 c++ 13238 Bitcoin investment  (0) 2022.09.26
백준 c++ 16460 Cupid  (0) 2022.08.21
백준 c++ 15312 이름 궁합  (0) 2022.08.17
백준 c++ 19709 LunchBox  (0) 2022.08.16