※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 4593번 문제인 Rock, Paper, Scissors이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/4593 

 

4593번: Rock, Paper, Scissors

Rock, Paper, Scissors is a classic hand game for two people. Each participant holds out either a fist (rock), open hand (paper), or two-finger V (scissors). If both players show the same gesture, they try again. They continue until there are two different

www.acmicpc.net

문자열을 둘씩 읽고 각 대응되는 차례의 두 문자를 살피면서 각 경기가 무승부인지 플레이어1이 이겼는지 플레이어2가 이겼는지를 판단해 문제를 해결하자.

 

세 가지 경우 중 둘에 해당하지 않으면 나머지 한 경우에 해당함을 이용하면 구현량을 줄일 수 있다.

 

아래는 제출한 소스코드이다.

#include <iostream>
#include <string>
using namespace std;

int slen;
string s1, s2;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> s1 >> s2;
	while (s1 != "E") {
		slen = s1.length();
		int ans1 = 0, ans2 = 0;
		for (int i = 0; i < slen; i++) {
			if (s1[i] == s2[i]) continue;
			if (s1[i] == 'R' && s2[i] == 'S') ans1++;
			else if (s1[i] == 'S' && s2[i] == 'P') ans1++;
			else if (s1[i] == 'P' && s2[i] == 'R') ans1++;
			else ans2++;
		}

		cout << "P1: " << ans1 << '\n' << "P2: " << ans2 << '\n';

		cin >> s1 >> s2;
	}
}
728x90

+ Recent posts