
내가 푼 코드
class Solution {
// 최대 공약수 구하는 메소드
public static int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// 최대 공배수 구하는 메소드
public static int lcmWithSix(int b) {
int a = 6; // 고정된 숫자 6 >> 피자 조각 수
return Math.abs(a * b) / gcd(a, b); // |6 * b| / GCD(6, b)
}
public int solution(int n) {
// 최대 공배수(필요한 피자 조각 수) / 6 >> 피자 판 개수
int answer = lcmWithSix(n) / 6;
return answer;
}
}
다른 코드
class Solution {
public int solution(int n) {
int answer = 0;
// i >> 피자 판수
// 인당 최소 1조각씩 >> i <= 6 * n로 최대치 설정
for (int i = 1; i <= 6 * n; i++) {
if (6 * i % n == 0) {
answer = i;
break;
}
}
return answer;
}
}
Share article