본문 바로가기
CodingTest/백준

[c++] 백준 - 2621: 카드게임

by Daybreak21 2023. 5. 19.

2621번: 카드게임 (acmicpc.net)

 

2621번: 카드게임

근우는 오늘 재미있는 카드 게임을 배우고 있다. 카드는 빨간색, 파란색, 노란색, 녹색의 네 가지 색이 있고, 색깔별로 1부터 9까지 숫자가 쓰여진 카드가 9장씩 있다. 카드는 모두 36(=4x9)장이다.

www.acmicpc.net

 

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;


int main() {
	char tmp; int color[4] = { 0, }, num[5] = { 0, }, result = 0, numArr[10] = { 0, };
	for (int i = 0; i < 5; i++) {
		cin >> tmp; cin >> num[i];

		numArr[num[i]]++;

		switch (tmp) {
		case 'R':
			color[0]++;
			break;
		case 'B':
			color[1]++;
			break;
		case 'Y':
			color[2]++;
			break;
		case 'G':
			color[3]++;
			break;
		}
	} //입력

	//같은 색이 5장인 경우 , 연속
	sort(num, num + 5);
	for (int i = 0; i < 4; i++) {
		bool colorBool = 0, cntBool = 0;
		if (color[i] == 5) colorBool = 1;
		if (num[1] - num[0] == 1 && num[2] - num[1] == 1 && num[3] - num[2] == 1 && num[4] - num[3] == 1) cntBool = 1; //반복문밖으로빼도됨

		if (colorBool && cntBool) { result = 900 + num[4]; } // #1
		else if (colorBool) { if (result < 600) result = 600 + num[4]; } // #4
		else if (cntBool) { if (result < 500) result = 500 + num[4]; } // #5
	}

	//같은 숫자
	for (int i = 0; i < 10; i++) {
		if (numArr[i] == 4) { if (result < 800) result = 800 + i; } // #2
		if (numArr[i] == 3) {
			if (result < 400) { result = 400 + i; } // #6
			for (int j = 0; j < 10; j++) if (numArr[j] == 2) { if (result < 700) result = 700 + i * 10 + j; } // #3
		}
		if (numArr[i] == 2) {
			if (result < 200) { result = 200 + i; } // #8
			for (int j = i + 1; j < 10; j++) if (numArr[j] == 2) { if (result < 300) result = (i > j) ? 300 + i * 10 + j : 300 + j * 10 + i; } // #7
		}
	}

	if (!(result)) result = 100 + num[4];

	cout << result;

	return 0;
}

같은 색의 카드, 연속인경우와 같은 숫자의 경우를 나눠서 검사하였다. (데이터도)

 

6번과 3번 조건과 같이 3번은 6번조건에 포함되기때문에 6번의 반복문 안에서 처리해주었다. 

카드를 입력받을때 데이터정리(sort제외)도 같이 되어서  코드를 직관적으로 작성하기 쉬웠다.