구현(Implementation)

백준 5789번: 한다 안한다

cepiloth 2018. 7. 23. 13:26
반응형

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


1. 문제

옛날에는 결정하기 어려운 일이 있을 때는 꽃을 이용해서 결정을 내렸다. 꽃을 하나 떼서 잎을 하나씩 떼면서, 한다와 안한다를 번갈아 가면서 말하다가 마지막 잎을 뗄 때 말한 말로 결정을 했다.


상근이는 이 방법을 응용해서 결정하기 어려운 일을 하나 결정하려고 한다.


먼저, 0과 1로 이루어진 문자열을 랜덤으로 하나 만든다. 그 다음 문자열의 양 끝에서 수를 하나씩 고르고, 두 수를 비교한다. 수가 같으면 "한다"이고, 다르면 "안한다"이다. 그 다음에는 고른 수를 버리고, 모든 수를 고를 때까지 이 작업을 반복한다. 따라서, 마지막으로 고르는 두 숫자로 결정을 내리는 것이다.


0과 1로 이루어진 문자열이 주어졌을 때, 상근이가 내리는 결정을 구하는 프로그램을 작성하시오.


2. 알고리즘

키워드 - 구현, 그리디


3. 코드


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <stack>
#include <math.h>
 
using namespace std;
 
#define MAX_SIZE 100
#define INF 0x7fffffff
#define CENDL "\n"
#define ll long long
 
int table[501];
int main() {
 
    cin.tie(0);
    std::ios::sync_with_stdio(false);
 
    int t; cin >> t;
 
    while(t--) {
        string s; cin >> s;
 
        const int size = s.size();
 
        char cand  = s[size/2-1];
        char cand2 = s[size/2];
        cout << "Do-it";
        if (cand != cand2) {
            cout << "-Not";
        }
        cout << CENDL;
    }
    return 0;
}
 
cs

반응형