백준

백준 소스코드 [C++] 12101 1, 2, 3 더하기 2

Hani_Levenshtein 2020. 10. 28. 07:49

www.acmicpc.net/problem/12101

 

12101번: 1, 2, 3 더하기 2

n을 1, 2, 3의 합으로 나타내는 방법 중에서 사전 순으로 k번째에 오는 것을 출력한다. k번째 오는 식이 없는 경우에는 -1을 출력한다.

www.acmicpc.net

백준 소스코드 [C++] 12101 1, 2, 3 더하기 2

#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
typedef long long ll;
using namespace std;
int k=0,m,n;
vector<int>v;
void dfs(int n) {
	if (n < 0) return;
	else if (n == 0) {
		k++; 
		if (k == m) {
			for (int i = 0;i < v.size() - 1;i++)cout << v[i] << "+";
			cout << v[v.size() - 1];
		}
		return;
	}
	else {
		v.push_back(1);
		dfs(n - 1);
		v.pop_back();
		v.push_back(2);
		dfs(n - 2);
		v.pop_back();
		v.push_back(3);
		dfs(n - 3);
		v.pop_back();
	}
}

int main() {
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	cin >> n >> m;
	dfs(n);
	if(k<m)cout << "-1";
	return 0;
}