Algorithm/COS PRO 1급 기출문제
[COS PRO 1급 기출문제 - Java] 1-9 계단 게임
goakgoak
2020. 12. 21. 13:37
문제 유형
코드 수정하기
문제
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
class Main {
public int func(int record){
if(record == 0) return 1;
else if(record == 1) return 2;
return 0;
}
public int solution(int[] recordA, int[] recordB){
int cnt = 0;
for(int i = 0; i < recordA.length; i++){
if(recordA[i] == recordB[i])
continue;
else if(recordA[i] == func(recordB[i]))
cnt = cnt + 3;
else
cnt = cnt - 1;
}
return cnt;
}
public static void main(String[] args) {
Main sol = new Main();
int[] recordA = {2,0,0,0,0,0,1,1,0,0};
int[] recordB = {0,0,0,0,2,2,0,2,2,2};
int ret = sol.solution(recordA, recordB);
System.out.println("solution 함수의 반환값은 " + ret + " 입니다.");
}
}
|
cs |
풀이
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
class Main {
public int func(int record){
if(record == 0) return 1;
else if(record == 1) return 2;
return 0;
}
public int solution(int[] recordA, int[] recordB){
int cnt = 0;
// 0 : 가위, 1 : 바위, 2 : 보
for(int i = 0; i < recordA.length; i++){
if(recordA[i] == recordB[i])
continue;
else if(recordA[i] == func(recordB[i]))
cnt = cnt + 3;
else
cnt = Math.max(0, cnt - 1);
}
return cnt;
}
public static void main(String[] args) {
Main sol = new Main();
int[] recordA = {2,0,0,0,0,0,1,1,0,0};
int[] recordB = {0,0,0,0,2,2,0,2,2,2};
int ret = sol.solution(recordA, recordB);
System.out.println("solution 함수의 반환값은 " + ret + " 입니다.");
}
}
|
cs |