Array_가위바위보해서 이긴사람 출력하기_switch
✅ 1: 가위, 2: 바위, 3: 보 일 때 이긴 사람 출력하기
첫 줄은 몇 번 가위바위보 했는지
둘째 줄은 A가 낸 손
셋째 줄은 B가 낸 손
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Main {
public String solution(int n, int[] arrayA, int[] arrayB){
String answer= "";
for(int i=0; i<n; i++){
int A= arrayA[i];
int B= arrayB[i];
if(A==B) answer += "D" ;
else if(A==1 && B==3) answer += 'A';
else if(A==2 && B==1) answer += 'A';
else if(A==3 && B==2) answer += 'A';
else answer += 'B';
}
return answer;
}
public static void main(String[] args) {
Main T = new Main();
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();
int[] arrayA= new int[n];
int[] arrayB= new int[n];
for(int i= 0; i<n; i++){
arrayA[i] = sc.nextInt();
}
for(int i= 0; i<n; i++){
arrayB[i] = sc.nextInt();
}
for(char c : T.solution(n, arrayA, arrayB).toCharArray()){ //string을 array로 바꿔
System.out.println(c);
}
}
}
//⭐️input:
// 5
// 2 3 3 1 3
// 1 1 2 2 3
//⭐️output:
// A
// B
// A
// B
// D
🔵 ThingsILearned
✔️ 줄 바꿔서 출력하기
알고리즘은 if를 사용해서 잘 짰는데, 줄 바꿔서 출력하는 것에서 막혔다.
출력하는 것은 solution함수가 아니라 psvm에서 해보도록 노력하자.
그리고 string에서는 바로 char을 배열로 뽑아낼 수 없으므로 string을 array로 바꿔주는 과정이 필요하다.
🟢 switch로 풀어보기
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Main {
public String solution(int n, int[] arrayA, int[] arrayB){
String answer= "";
for(int i=0; i<n; i++){
int A= arrayA[i];
int B= arrayB[i];
switch(A - B ){
case -2 : case 1: {
answer +="A";
break;
}
case -1 : case 2: {
answer +="B";
break;
}
case 0:{
answer += "D";
break;
}
}
}
return answer;
}
public static void main(String[] args) {
Main T = new Main();
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();
int[] arrayA= new int[n];
int[] arrayB= new int[n];
for(int i= 0; i<n; i++){
arrayA[i] = sc.nextInt();
}
for(int i= 0; i<n; i++){
arrayB[i] = sc.nextInt();
}
for(char c : T.solution(n, arrayA, arrayB).toCharArray()){
System.out.println(c);
}
}
}
This post is licensed under CC BY 4.0 by the author.