728x90
https://www.acmicpc.net/problem/2480
- 예제 입력1
3 3 6
- 예제 출력1
1300
- 예제 입력2
2 2 2
- 예제 출력2
12000
- 예제 입력3
6 2 5
- 예제 출력3
600
- 문제 접근
- 주사위의 눈 (1~6)으로 푸는 문제
- 같은 눈이 2개일 경우 같은 눈의 값이 몇 인지 알아야 함
- 모두 다른 눈일 경우 가장 큰 값을 구해야 함
- 문제 해결
- Counting sort를 이용하여 문제를 풀 수 있을 것 같음
- 각 나온 수의 인덱스를 1씩 증가 시킴
- 특정 인덱스의 값이 3일 경우 같은 눈 3
- 특정 인덱스의 값이 2일 경우 같은 눈 2
- 인덱스의 Max 값이 1일 경우 가장 높은 인덱스 출력
- 슈도 코드
A[7] 배열 생성
for(i = 0 ~ 2까지 반복){
A 배열에 주사위 눈 인덱스 1증가
}
max(A배열의 최대값 저장)
diceValue = searchsearchDiceValue(A, max)
res(결과 저장)
if(max가 1이라면)
res = diceValue * 6
else if(max가 2라면)
res = 1000 + (diceValue * 100);
else
res = 10000 + (diceValue * 1000);
res 출력
searchDiceValue(A, max){
for(i = 배열 길이-1 ~ 1까지 반복){
if(max와 인덱스 값이 같으면)
i 반환
}
}
- 코딩하기
public static void main(String[] args) {
int[] A = new int[7];
final int DICE_COUNT = 3;
Scanner scan = new Scanner(System.in);
for(int i = 0; i < DICE_COUNT; i ++){
A[scan.nextInt()]++;
}
scan.close();
int max = Arrays.stream(A).max().getAsInt(); // 배열에서 가장 높은 값 저장
int diceValue = searchDiceValue(A, max); // 계산할 dice 값 추출 함수
int res = 0;
if(max == 1)
res = diceValue * 100;
else if(max == 2)
res = 1000 + (diceValue * 100);
else
res = 10000 + (diceValue * 1000);
System.out.println(res);
}
private static int searchDiceValue(int[] A, int max){
// 모두 다른 눈일 경우 최댓값을 구해야하기 때문에
// i = 배열의 마지막 인덱스부터 탐색
for(int i = A.length-1; i >= 1; i--){
if(A[i] == max)
return i;
}
return -1;
}
갑작스럽게 계수 정렬로 풀 수 있을 거 같다고 생각해서 시도해보긴 했는데 독특한 풀이 방법 같아서 괜찮은 느낌이였음
728x90