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
- 시뮬레이션
- 수학
- XR Interaction Toolkit
- 다이나믹 프로그래밍
- 그리디 알고리즘
- VR
- 정렬
- 투 포인터
- 그래프
- 자료구조
- 재귀
- 트리
- 문자열
- 누적 합
- c++
- 백준
- DFS
- 유니온 파인드
- 유니티
- Unreal Engine 5
- 브루트포스
- 알고리즘
- 구현
- ue5
- 다익스트라
- 백트래킹
- BFS
- Team Fortress 2
- 우선순위 큐
- 스택
Archives
- Today
- Total
1일1알
백준 14225번 부분수열의 합 C++ 본문
수열의 크기는 최대 20이고, 수열을 이루는 각 수가 최대 100000이기 때문에 나올 수 있는 최대의 수는 2000000이다.
2000000개의 크기를 갖는 배열을 만들어서 재귀함수를 통해 나온 수들의 합을 저장하고, 배열을 1부터 탐색하면서 만약 저장되지 않은 수를 찾는다면 탐색을 멈추고 탐색이 멈춘 부분을 출력하는 방식으로 문제를 해결하였다.
#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 <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
vector<int> v;
bool ans[2000001] = { false };
void solve(int index, int val) {
for (int i = index; i < v.size(); i++) {
ans[v[i] + val] = true;
solve(i + 1, val + v[i]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, input;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> input;
v.push_back(input);
}
solve(0, 0);
int key = 1;
for (int i = 1; i < 2000001; i++) {
if (ans[i] == false) {
key = i;
break;
}
}
cout << key;
};
'알고리즘' 카테고리의 다른 글
백준 6118번 숨바꼭질 C++ (0) | 2021.11.18 |
---|---|
백준 13335번 트럭 C++ (0) | 2021.11.17 |
백준 17609번 회문 C++ (1) | 2021.11.15 |
백준 1254번 팰린드롬 만들기 C++ (0) | 2021.11.14 |
백준 2961번 도영이가 만든 맛있는 음식 C++ (0) | 2021.11.13 |