728x90
Topcoder Arena
Topcoder Arena Web
arena.topcoder.com
문제 파악하기
1. 같은 행동을 N번 실행하는 것이므로 재귀함수를 사용하자.
A, B, C에서 하나를 고르는 행동을 N번 하는 것이기 때문에, 하나를 고르는 함수를 하나 만드는 것이 좋다고 생각하였다. 하나를 고르면 해당 숫자가 -1 줄고(1보다 작으면 줄지 않음), 그 숫자들을 다시 함수에 넣어 N번 실행한다.
2. A, B, C 중 가장 큰 숫자를 찾는 함수를 만들자.
사실 for문을 사용해도 되는데, 그냥 존재하는 세 개의 수 중에서 가장 큰 값의 알파벳과 그 숫자값을 가져오는 함수를 한 ㅏ또 만들었다.
(Java)
public class AdditionGame {
public int getMaximumPoints(int A, int B, int C, int N) {
return game(A,B,C,1,0,N);
}
public int game(int A, int B, int C, int count, int totalScore,int N) {
if (count > N) return totalScore;
System.out.println(count + "번 째 선택입니다. 현재 점수: " + totalScore);
int[] max = findMax(A,B,C);
totalScore += max[1];
System.out.println("점수를 획득하셨습니다! 현재 점수: " + totalScore);
if (max[0] == 1 && A>=1) A--;
else if (max[0] == 2 && B>=1) B--;
else if (max[0] == 3 && C>=1) C--;
count ++;
return game(A,B,C,count, totalScore,N);
}
public int[] findMax (int a, int b, int c) {
int[] max = new int[3];
max[1] = Math.max(Math.max(a, b), c);
if (a == max[1]) max[0] = 1;
else if(b == max[1]) max[0] = 2;
else max[0] = 3;
System.out.println("현재 가장 큰 숫자: " + max[1]);
return max;
}
}
728x90
'Algorithm Study > TopCoder' 카테고리의 다른 글
| [TopCoder] BrokenStrings (C#, Java) (2) | 2023.10.10 |
|---|---|
| [TopCoder] 회문 (Java, C#) (0) | 2023.08.13 |
| [TopCoder] InterestingDigits (Java, C#) (0) | 2023.08.12 |
| [TopCoder] Cryptography (C#, Java) (0) | 2023.08.12 |
| [TopCoder] InterestingParty (C#, Java) (0) | 2023.08.11 |