Algorithm/Algorithms Practice
[BOJ/C++] 카드게임(10801번)
Patrick_
2022. 10. 14. 14:45
1. 개요
https://www.acmicpc.net/problem/10801
10801번: 카드게임
두 사람 A와 B는 1부터 10까지의 숫자가 하나씩 적힌 열 장의 카드로 ‘게임’을 한다. 게임은 총 열 번의 ‘라운드’로 구성되고, 각 라운드 마다 자신이 가지고 있는 카드 중 하나를 제시하고,
www.acmicpc.net
1-1. 설명
두 사람 A, B는 1부터 10까지 숫자가 하나씩 적힌 열장 카드로 게임을 한다. 게임의 규칙은 위의 링크에서 확인이 가능하다. A와 B가 각 라운드마다 제시한 숫자와 각 라운드의 승자를 계산하여, 누가 이겼는지 출력하는 프로그램을 작성한다.
1-2. 제한 사항
- 첫줄에는 A가 제시한 카드 숫자 10개가 라운드 순서대로 주어짐
- 둘째줄에는 B가 제시한 카드 숫자 10개가 라운드 순서대로 주어짐
2. 구현
2-1. 풀이
각각의 A, B가 제시한 카드를 배열에 저장하고 for문을 통해 비교해주었다.
2-2. 코드
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int A[10], B[10];
int countA = 0, countB = 0;
void split(string s, char delimiter, int flag) {
stringstream ss(s);
string str;
if (flag == 1) {
while (getline(ss, str, delimiter)) {
A[countA++] = stoi(str);
}
countA = 0;
}
else if (flag == 2) {
while (getline(ss, str, delimiter)) {
B[countB++] = stoi(str);
}
countB = 0;
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string strA, strB;
getline(cin, strA);
getline(cin, strB);
split(strA, ' ', 1);
split(strB, ' ', 2);
for (int i = 0; i < 10; i++) {
if (A[i] > B[i]) countA += 1;
else if (A[i] < B[i]) countB += 1;
}
if (countA > countB) cout << 'A';
else if (countA < countB) cout << 'B';
else cout << 'D';
return 0;
}