문제설명
1부터 6까지 숫자가 적힌 주사위가 네 개 있습니다. 네 주사위를 굴렸을 때 나온 숫자에 따라 다음과 같은 점수를 얻습니다
- 네 주사위에서 나온 숫자가 모두 p로 같다면 1111 × p점을 얻습니다.
- 세 주사위에서 나온 숫자가 p로 같고 나머지 다른 주사위에서 나온 숫자가 q(p ≠ q)라면 (10 × p + q)2 점을 얻습니다.
- 주사위가 두 개씩 같은 값이 나오고, 나온 숫자를 각각 p, q(p ≠ q)라고 한다면 (p + q) × |p - q|점을 얻습니다.
- 어느 두 주사위에서 나온 숫자가 p로 같고 나머지 두 주사위에서 나온 숫자가 각각 p와 다른 q, r(q ≠ r)이라면 q × r점을 얻습니다.
- 네 주사위에 적힌 숫자가 모두 다르다면 나온 숫자 중 가장 작은 숫자 만큼의 점수를 얻습니다.
네 주사위를 굴렸을 때 나온 숫자가 정수 매개변수 a, b, c, d로 주어질 때, 얻는 점수를 return 하는 solution 함수를 작성해 주세요.
제한사항
a, b, c, d는 1 이상 6 이하의 정수입니다.
입출력 예
a | b | c | d | result |
2 | 2 | 2 | 2 | 2222 |
4 | 1 | 4 | 4 | 1681 |
6 | 3 | 3 | 6 | 27 |
2 | 5 | 2 | 6 | 30 |
6 | 4 | 2 | 5 | 2 |
입출력 예 설명
입출력 예 #1
예제 1번에서 네 주사위 숫자가 모두 2로 같으므로 1111 × 2 = 2222점을 얻습니다. 따라서 2222를 return 합니다.
입출력 예 #2
예제 2번에서 세 주사위에서 나온 숫자가 4로 같고 나머지 다른 주사위에서 나온 숫자가 1이므로 (10 × 4 + 1)2 = 412 = 1681점을 얻습니다. 따라서 1681을 return 합니다.
입출력 예 #3
예제 3번에서 a, d는 6으로, b, c는 3으로 각각 같으므로 (6 + 3) × |6 - 3| = 9 × 3 = 27점을 얻습니다. 따라서 27을 return 합니다.
입출력 예 #4
예제 4번에서 두 주사위에서 2가 나오고 나머지 다른 두 주사위에서 각각 5, 6이 나왔으므로 5 × 6 = 30점을 얻습니다. 따라서 30을 return 합니다.
입출력 예 #5
예제 5번에서 네 주사위 숫자가 모두 다르고 나온 숫자 중 가장 작은 숫자가 2이므로 2점을 얻습니다. 따라서 2를 return 합니다.
풀이
JAVA
class Solution {
public int solution(int a, int b, int c, int d) {
int answer = 0;
if(a == b) {
if(a == c) {
if(a == d) {
answer = 1111 * a;
} else {
answer = (int)Math.pow(10*a+d, 2);
}
} else if(a == d) {
answer = (int)Math.pow(10*a+c, 2);
} else {
if(c == d) {
answer = (a + c) * Math.abs(a - c);
} else {
answer = c * d;
}
}
} else if(a == c) {
if(a == d) {
answer = (int)Math.pow(10*a+b, 2);
} else {
if(b == d) {
answer = (a + b) * Math.abs(a - b);
} else {
answer = b * d;
}
}
} else if(a == d) {
if(b == c) {
answer = (a + b) * Math.abs(a - b);
} else {
answer = b * c;
}
} else {
if(b == c) {
if(c == d) {
answer = (int)Math.pow(10*b+a, 2);
} else {
answer = a * d;
}
} else if(b == d) {
answer = a * c;
} else if(c == d) {
answer = a * b;
} else {
answer = Math.min(Math.min(a, b), Math.min(c, d));
}
}
return answer;
}
}
* 깔끔 코드
import java.util.Arrays;
class Solution {
public int solution(int a, int b, int c, int d) {
int[] dice = { a, b, c, d };
Arrays.sort(dice);
int ans = 0;
if (dice[0] == dice[3]) {
ans = 1111 * dice[3];
} else if (dice[0] == dice[2] || dice[1] == dice[3]) {
ans = (int) Math.pow(dice[1] * 10 + (dice[0] + dice[3] - dice[1]), 2);
} else if (dice[0] == dice[1] && dice[2] == dice[3]) {
ans = (dice[0] + dice[3]) * (dice[3] - dice[0]);
} else if (dice[0] == dice[1]) {
ans = dice[2] * dice[3];
} else if (dice[1] == dice[2]) {
ans = dice[0] * dice[3];
} else if (dice[2] == dice[3]) {
ans = dice[0] * dice[1];
} else {
ans = dice[0];
}
return ans;
}
}
Python
def solution(a, b, c, d):
nums = [a, b, c, d]
counts = [nums.count(i) for i in nums]
if max(counts) == 4:
return a * 1111
elif max(counts) == 3:
p = nums[counts.index(3)]
q = nums[counts.index(1)]
return (10 * p + q) ** 2
elif max(counts) == 2:
if min(counts) == 2:
return (a + c) * abs(a - c) if a == b else (a + b) * abs(a - b)
else:
p = nums[counts.index(2)]
return (a * b * c * d) / p**2
else:
return min(nums)
JS
function solution(a, b, c, d) {
if (a === b && a === c && a === d) return 1111 * a
if (a === b && a === c) return (10 * a + d) ** 2
if (a === b && a === d) return (10 * a + c) ** 2
if (a === c && a === d) return (10 * a + b) ** 2
if (b === c && b === d) return (10 * b + a) ** 2
if (a === b && c === d) return (a + c) * Math.abs(a - c)
if (a === c && b === d) return (a + b) * Math.abs(a - b)
if (a === d && b === c) return (a + b) * Math.abs(a - b)
if (a === b) return c * d
if (a === c) return b * d
if (a === d) return b * c
if (b === c) return a * d
if (b === d) return a * c
if (c === d) return a * b
return Math.min(a, b, c, d)
}
* 다른 코드
function solution(a, b, c, d) {
var answer = 0;
const obj = [...arguments].reduce((p,c) => {
if(!p.hasOwnProperty(c)) p[c] = 0;
p[c]++;
return p;
},{});
switch(Object.keys(obj).length) {
case 1 :
return 1111 * Object.keys(obj)[0];
case 2 :
const values = Object.values(obj);
if(values[0] == 2) {
const n1 = parseInt(Object.keys(obj)[0]);
const n2 = parseInt(Object.keys(obj)[1]);
return (n1 + n2) * Math.abs(n1 - n2);
} else {
return Object.entries(obj).reduce((p,c) => {
const max = parseInt(p[1] == 3 ? p[0] : c[0]);
const min = parseInt(p[1] == 3 ? c[0] : p[0]);
return Math.pow(10 * max + min,2);
});
}
case 3 :
return Object.entries(obj)
.filter(e => e[1] == 1)
.map(e => parseInt(e[0]))
.reduce((p,c) => p * c);
case 4 :
return Math.min(...Object.keys(obj).map(e => parseInt(e)))
}
return answer;
}
'프로그래머스 Lv.0' 카테고리의 다른 글
Lv.0 9로 나눈 나머지 (0) | 2025.02.04 |
---|---|
Lv.0 글자 이어 붙여 문자열 만들기 (0) | 2025.02.03 |
Lv.0 간단한 논리 연산 (1) | 2025.02.02 |
Lv.0 배열 만들기 4 (0) | 2025.02.02 |
Lv.0 콜라츠 수열 만들기 (0) | 2025.02.01 |