KSI일기장
프로그래머스 피자 나눠먹기(2) 자바 본문
풀이 1
import java.util.ArrayList;
class Solution {
public int solution(int n) {
int answer = 0;
int slice = 6;
ArrayList<Integer> anArr = new ArrayList<>();
if (n == 1 || n == 2){ //n(인원수) == 1 or 2 인 경우
answer = 1;
} else if(n<slice){ //n(인원수) = 1~5 인경우 (1,2는 제외)
if (n%slice > 0) { //n%6>0 인 경우 (n이 6의 배수가 아닌 경우)
for (int i=1; i<slice; i++){
for(int j=1; j<=slice; j++) {
if((i*slice) == (j*n)){ //i*6 == j*n(인원수) 인 경우
anArr.add(i); //리스트 anArr에 피자 판 수 될 (i)를 추가
answer = anArr.get(0); //가장 첫번째 원소(두 수의 최소공배수의 최소공약수)
break;
}
}
}
}
}else if(n>=slice){ //n(인원수)>=6 인 경우
if (n%slice > 0) { //n(인원수)%6>0 인 경우 (n(인원수)이 6의 배수가 아닌 경우)
for (int i=1; i<=n; i++){
for(int j=1; j<=slice; j++) {
if((i*slice) == (j*n)){
anArr.add(i);
answer = anArr.get(0);
break;
}
}
}
}else{ //n%6 == 0 인 경우(n이 6의 배수)
answer = n/slice;
}
}
return answer;
}
}
풀이 2
class Solution {
public int solution(int n) {
int answer = 1;
while(true){
if(6*answer%n==0) break;
answer++;
}
return answer;
}
}
풀이 3
class Solution {
public int solution(int n) {
int answer = 0;
for (int i = 1; i <= 6 * n; i++) {
if (6 * i % n == 0) {
answer = i;
break;
}
}
return answer;
}
}
'JavaAlgorithm' 카테고리의 다른 글
프로그래머스 369게임 (자바) (0) | 2023.12.14 |
---|---|
프로그래머스 숫자 찾기 (자바) (0) | 2023.12.14 |
프로그래머스 약수 구하기(자바) (1) | 2023.12.07 |
프로그래머스 외계행성의 나이(자바) (1) | 2023.12.07 |
프로그래머스 배열 회전시키기 (자바) (1) | 2023.12.06 |