<섹션2-ARRAY> 3. 가위 바위 보

조이·2021년 7월 28일
0

자바 알고리즘

목록 보기
15/41
post-thumbnail

3. 가위 바위 보

<설명>

A, B 두 사람이 가위바위보 게임을 합니다. 총 N번의 게임을 하여 A가 이기면 A를 출력하고, B가 이기면 B를 출력합니다. 비길 경우에는 D를 출력합니다.

가위, 바위, 보의 정보는 1:가위, 2:바위, 3:보로 정하겠습니다.

두 사람의 각 회의 가위, 바위, 보 정보가 주어지면 각 회를 누가 이겼는지 출력하는 프로그램을 작성하세요.

<입력>

첫 번째 줄에 게임 횟수인 자연수 N(1<=N<=100)이 주어집니다.

두 번째 줄에는 A가 낸 가위, 바위, 보 정보가 N개 주어집니다.

세 번째 줄에는 B가 낸 가위, 바위, 보 정보가 N개 주어집니다.

<출력>

각 줄에 각 회의 승자를 출력합니다. 비겼을 경우는 D를 출력합니다.

===================================================

<코드>

나올 수 있는 경우의 수를 모두 고려하여 if문을 활용하여 답을 구해야 한다.

import java.util.Scanner;

public class Main {
	public char[] solution(int order,int[] A,int[] B) {
		char [] answer = new char[order];
		for(int i=0;i<order;i++) {
			if(A[i]==B[i]) answer[i]='D';
			else if(A[i]==1 && B[i]==3) answer[i]='A'; 
			else if(A[i]==2 && B[i]==1) answer[i]='A'; 
			else if(A[i]==3 && B[i]==2) answer[i]='A'; 
			else answer[i]='B';
		}

		return answer;
	}
	
	
	
	public static void main(String[] args) {
		Main main = new Main();
		Scanner scan = new Scanner(System.in);
		int order=scan.nextInt();
		int [] A= new int[order];
		int [] B= new int[order];
		for(int i=0;i<order;i++) {
			A[i]=scan.nextInt();
		}
		for(int i=0;i<order;i++) {
			B[i]=scan.nextInt();
		}
		for(char x :main.solution(order,A,B))
			System.out.println(x);
		
	}
}
profile
joy_study

0개의 댓글