Notice
Recent Posts
Recent Comments
Link
«   2025/10   »
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
Archives
Today
Total
관리 메뉴

코딩로그

[백준/BOJ/C++] 4493번 가위 바위 보? 본문

YJ/C++

[백준/BOJ/C++] 4493번 가위 바위 보?

Team DAON 2021. 10. 18. 16:58

[문제]

가위 바위 보는 두 명이서 하는 게임이다. 보통 미리 정해놓은 수 만큼 게임을 하고, 많은 게임을 이긴 사람이 최종 승자가 된다.

가위 바위 보를 한 횟수와 매번 두 명이 무엇을 냈는지가 주어졌을 때, 최종 승자를 출력하는 프로그램을 작성하시오.

  • 바위는 가위를 이긴다.
  • 가위는 보를 이긴다.
  • 보는 바위를 이긴다.

 

[입력]

첫째 줄에는 테스트 케이스의 개수 t(0 < t < 1000)가 주어진다. 각 테스트 케이스의 첫째 줄에는 가위 바위 보를 한 횟수 n(0 < n < 100)이 주어진다. 다음 n개의 줄에는 R, P, S가 공백으로 구분되어 주어진다. R, P, S는 순서대로 바위, 보, 가위이고 첫 번째 문자는 Player 1의 선택, 두 번째 문자는 Player 2의 선택이다.

 

[출력]

각 테스트 케이스에 대해서 승자를 출력한다. (Player 1 또는 Player 2) 만약, 비겼을 경우에는 TIE를 출력한다.

 

[Source Code]

#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;

int main() {
	int test;
	cin >> test;

	int *arr = new int[test];

	for(int i = 0; i < test; i++){
		int number, win_player1 = 0, win_player2 = 0;
		cin >> number;

		for(int j = 0; j < number; j++){
			char player1, player2;
			cin >> player1 >> player2;

			if(player1 == 'R'){
				if(player2 == 'S') win_player1++;
				else if(player2 == 'P') win_player2++;
			}
			else if(player1 == 'S'){
				if(player2 == 'R') win_player2++;
				else if(player2 == 'P') win_player1++;
			}
			else{
				if(player2 == 'S') win_player2++;
				else if(player2 == 'R') win_player1++;
			}
		}

		if(win_player1 == win_player2)
			arr[i] = 0;
		else if(win_player1 < win_player2)
			arr[i] = 2;
		else
			arr[i] = 1;
	}

	for(int i = 0; i < test; i++){
		if(arr[i] == 0)
			cout << "TIE";
		else if(arr[i] == 1)
			cout << "Player 1";
		else
			cout << "Player 2";
		cout << "\n";
	}
}

[결과 화면]