-
프로그래머스 - 종이접기알고리즘 풀이/프로그래머스 2020. 4. 11. 19:13
문제 :
https://programmers.co.kr/learn/courses/30/lessons/62049
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이 :
종이를 반으로 접기 때문에 n이 증가할수록
(n-1)은 그대로 쓰이게 되고 ( 왼쪽 )
반으로 접기 때문에 가운데 부분에 0이 생기고 (가운데)
데칼코마니 형태로 오른쪽이 생기기 때문에 이전 종이(n-1)를 접었던 것에 순서를 뒤집고 1은 0으로 0은 1로 바꾸어서
처리해주었다. ( 오른쪽 )
코드 ( C++ )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters#include <string> #include <vector> #include <iostream> using namespace std; vector<int> rev(vector<int> begin) { vector<int> temp; for (int i = begin.size() - 1; i >= 0; --i) { temp.push_back(1 - begin[i]); } return temp; } vector<int> solution(int n) { vector<int> answer(1); if (n == 1) return answer; for (int i = 2; i <= n; ++i) { vector<int> temp = rev(answer); answer.push_back(0); for (int j = 0; j < temp.size(); ++j) answer.push_back(temp[j]); } return answer; } '알고리즘 풀이 > 프로그래머스' 카테고리의 다른 글
프로그래머스 - 베스트앨범 (0) 2020.01.01 프로그래머스 - 다리를 지나는 트럭 (0) 2019.12.14 프로그래머스 - H-Index (0) 2019.12.08 프로그래머스 - 숫자 야구 (0) 2019.12.06 프로그래머스 - 위장 (0) 2019.12.05