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제외)도 같이 되어서 코드를 직관적으로 작성하기 쉬웠다.
'CodingTest > 백준' 카테고리의 다른 글
[c++] 백준 - 2577: 숫자의 개수 (0) | 2023.05.21 |
---|---|
[c++] 백준 - 2816: 디지털 티비 (0) | 2023.05.20 |
[c++] 백준 - 25206: 너의 평점은 (1) | 2023.05.16 |
[c++] 백준 - 1316: 그룹 단어 체커 (0) | 2023.05.16 |
[c++] 백준 - 1652: 누울 자리를 찾아라 (1) | 2023.05.10 |