본문 바로가기
스터디/알고리즘

[ 프로그래머스 ] 종이 접기

by 알 수 없는 사용자 2020. 2. 4.

사용 언어 : C++

 

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
#include <stdio.h>
#include <cmath>
#include <vector>
 
using namespace std;
 
vector<int> solution(int n) {
 
    vector<int> temp;
    vector<int> answer;
 
    for (int i = 0; i < n; ++i)
    {
        if (i == 0// n = 1
        {
            answer.push_back(0);
        }
        else // n > 1
        {
            temp.clear();
 
            int size = answer.size();// 본래 크기
 
            for (int j = 0; j < size++j)
            {
 
                temp.push_back(j % 2); // 답 추가
                temp.push_back(answer[j]); // 답 추가
 
                if (j == size - 1 || size == 1// 마지막 || n = 1
                {
                    temp.push_back((j + 1) % 2);
                }
 
            }
 
            answer = temp;
        }
        
    }
    return answer;
}