프로그래머스: 주사위 게임 2

김아무개·2023년 4월 29일
0

프로그래머스

목록 보기
35/41

다른 사람 코드

class Solution {
    public int solution(int a, int b, int c) {
        int answer = 1;

        int count = 1;
        if(a == b || a == c || b == c) {
            count++;
        }

        if(a == b && b == c) {
            count++;
        }

        for(int i = 1; i <= count; i++) {
            answer *= (pow(a,i)+pow(b,i)+pow(c,i));
        }

        return answer;
    }

    private int pow(int a, int b) {
        if(b == 0) return 1;
        return a * pow(a, b-1);
    }
}

우와 🙊

내 코드

class Solution {
    int[] arr;
    int a, b, c;
    public int solution(int a, int b, int c) {
        this.a = a; 
        this.b = b; 
        this.c = c;
        
        arr = new int[Math.max(a, Math.max(b, c)) + 1];
        arr[a]++;
        arr[b]++;
        arr[c]++;

        if (arr[a] == 1 && arr[b] == 1) {
            return pow(1);
        } else if (arr[a] == 3) {
            return pow(3);
        } else {
            return pow(2);
        }
    }
    private int pow(int p) {
        int num = 1;
        for (int i = 1; i <= p; i++)
            num *= Math.pow(a, i) + Math.pow(b, i) + Math.pow(c, i);
        return num;
    }
}
profile
Hello velog! 

0개의 댓글