일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- OS
- D2
- cs
- BOJ
- swea
- Computer Science
- LeetCode
- 자료구조
- c++
- 감상문
- data structure
- 운영체제
- 프로그래머스
- 재테크/투자
- algorithm
- db
- language
- network
- 데이터베이스
- Programmers
- 법의학
- D3
- 네트워크
- 백준
- 알고리즘
- SW Expert Academy
- 독서
- Database
- 문제풀이
- algogritim
Archives
- Today
- Total
선택은 나의 것
[BOJ 백준] 14225번 부분수열의 합 본문
문제
BOJ 14225 : https://www.acmicpc.net/problem/14225
14225번: 부분수열의 합
수열 S가 주어졌을 때, 수열 S의 부분 수열의 합으로 나올 수 없는 가장 작은 자연수를 구하는 프로그램을 작성하시오. 예를 들어, S = [5, 1, 2]인 경우에 1, 2, 3(=1+2), 5, 6(=1+5), 7(=2+5), 8(=1+2+5)을 만들 �
www.acmicpc.net
접근
2중 for문을 이용해 모든 경우의 수에 따른 계산 결과를 구해보았다. 이때 set을 사용해 중복되는 값은 걸러주고, set의 오름차순 정렬을 통해 1부터 체크하여 등장하지 않는 가장 작은 수를 구할 수 있었다.
코드
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
int n, num[30];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> num[i];
}
set<int> s;
for (int i = 0; i < n; i++) {
vector<int> add;
for (auto it = s.begin(); it != s.end(); it++) {
add.push_back(*it + num[i]);
}
s.insert(num[i]);
for (int j = 0; j < add.size(); j++) {
s.insert(add[j]);
}
}
int counter = 1;
for (auto it = s.begin(); it != s.end(); it++) {
if (*it != counter) {
cout << counter;
return 0;
}
counter++;
}
cout << counter;
}
'☽ Algorithm > BOJ' 카테고리의 다른 글
[BOJ 백준] 2857번 FBI (python) (0) | 2020.08.03 |
---|---|
[BOJ 백준] 16948번 데스 나이트 (0) | 2020.07.31 |
[BOJ 백준] 16917번 양념 반 후라이드 반 (0) | 2020.07.29 |
[BOJ 백준] 16968번 차량 번호판 1 (0) | 2020.07.29 |
[BOJ 백준] 1614번 영식이의 손가락 (0) | 2020.07.28 |
Comments