Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 알고리즘
- 백준
- 투 포인터
- 수학
- VR
- 유니티
- DFS
- c++
- XR Interaction Toolkit
- 트리
- 그래프
- 문자열
- 시뮬레이션
- Unreal Engine 5
- 다이나믹 프로그래밍
- 다익스트라
- 누적 합
- 구현
- ue5
- 정렬
- 재귀
- 유니온 파인드
- 스택
- Team Fortress 2
- 우선순위 큐
- 백트래킹
- BFS
- 브루트포스
- 그리디 알고리즘
- 자료구조
Archives
- Today
- Total
1일1알
백준 12101번 1, 2, 3 더하기 2 C++ 본문
https://www.acmicpc.net/problem/12101
12101번: 1, 2, 3 더하기 2
n을 1, 2, 3의 합으로 나타내는 방법 중에서 사전 순으로 k번째에 오는 것을 출력한다. k번째 오는 식이 없는 경우에는 -1을 출력한다.
www.acmicpc.net
dfs
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int n, k;
int cnt = 1;
bool ans = false;
vector<int> nums;
void Dfs(int sum) {
if (sum > n) return;
if (sum == n) {
if (cnt++ == k) {
ans = true;
for (int i = 0; i < nums.size(); i++) {
if (i != 0) cout << '+';
cout << nums[i];
}
}
return;
}
nums.push_back(1);
Dfs(sum + 1);
nums.pop_back();
nums.push_back(2);
Dfs(sum + 2);
nums.pop_back();
nums.push_back(3);
Dfs(sum + 3);
nums.pop_back();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> k;
Dfs(0);
if (ans == false) cout << -1;
}
'알고리즘' 카테고리의 다른 글
백준 16507번 어두운 건 무서워 C++ (0) | 2023.06.22 |
---|---|
백준 1431번 시리얼 번호 C++ (0) | 2023.06.21 |
백준 17610번 양팔저울 C++ (0) | 2023.06.09 |
백준 21278번 호석이 두 마리 치킨 C++ (1) | 2023.06.08 |
백준 3182번 한동이는 공부가 하기 싫어! C++ (0) | 2023.06.07 |