프로그래머스 코딩테스트 입문(C++)

프로그래머스 14) 피자 나눠 먹기 (3) (Lv. 0) (C++)

코테 2023. 3. 28. 00:39

전체 코드

#include <string>
#include <vector>

using namespace std;

int solution(int slice, int n) {
    int answer = 0;
    answer=n/slice;
    if(n%slice!=0)
        answer++;
    return answer;
}

풀이

  • n(사람수)/slice 하여 몇판 필요한지 구함
  • n%slice가 0이 아니라면 나머지가 있다는 뜻이다. int끼리 나누면 나머지는 버려진다. 따라서 나머지가 있다는 뜻은 자기몫이 들어오지 않은 사람이 있다는 뜻이다. 따라서 if문으로 따로 1판을 더 더해준다.
answer=n/slice;
if(n%slice!=0)
    answer++;