[Programmers] #120814 - 피자 나눠 먹기 (1) [Java][C++][Python]
[Programmers] #120814 - 피자 나눠 먹기 (1) [Java][C++][Python]
1. 아이디어
$n$명에게 한 조각씩 주려면 $n$조각이 필요하고 한 판이 7조각이므로, 답은 $n$을 7로 나눈 값의 올림이다. 정수 나눗셈의 몫은 내림이라, 7로 나누기 전에 6을 더하면 나머지가 1 이상일 때만 몫이 1 커져 올림이 된다.
2. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
class Solution {
public int solution(int n) {
return (n + 6) / 7;
}
}
1
2
3
4
5
6
#include <bits/stdc++.h>
using namespace std;
int solution(int n) {
return (n + 6) / 7;
}
1
2
def solution(n):
return (n + 6) // 7
This post is licensed under CC BY 4.0 by the author.