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 | 29 | 30 |
Tags
- 그리디 알고리즘
- 수학
- 자료구조
- 재귀
- Unreal Engine 5
- 누적 합
- 투 포인터
- DFS
- 그래프
- 구현
- 시뮬레이션
- 트리
- 유니티
- 유니온 파인드
- Team Fortress 2
- 백트래킹
- 스택
- 다이나믹 프로그래밍
- 문자열
- 브루트포스
- 정렬
- c++
- XR Interaction Toolkit
- VR
- 다익스트라
- ue5
- BFS
- 우선순위 큐
- 알고리즘
- 백준
Archives
- Today
- Total
1일1알
백준 2992번 크면서 작은 수 C++ 본문
https://www.acmicpc.net/problem/2992
2992번: 크면서 작은 수
정수 X가 주어졌을 때, X와 구성이 같으면서 X보다 큰 수 중 가장 작은 수를 출력한다. 수의 구성이 같다는 말은, 수를 이루고 있는 각 자리수가 같다는 뜻이다. 예를 들어, 123과 321은 수의 구성이
www.acmicpc.net
백트래킹을 이용해서 문제를 해결하였다.
#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;
string str;
int target;
int ans = 0;
vector<bool> visited;
vector<int> idxs;
void BT(int cnt) {
if (cnt >= str.length()) {
string currStr = "";
for (auto a : idxs) {
currStr += str[a];
}
int currNum = stoi(currStr);
if (currNum > target) {
if (ans == 0) ans = currNum;
else ans = min(ans, currNum);
}
return;
}
for (int i = 0; i < str.length(); i++) {
if (visited[i]) continue;
visited[i] = true;
idxs.push_back(i);
BT(cnt + 1);
visited[i] = false;
idxs.pop_back();
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> str;
target = stoi(str);
visited = vector<bool>(str.length(), false);
BT(0);
cout << ans;
}
'알고리즘' 카테고리의 다른 글
백준 14248번 점프 점프 C++ (0) | 2022.12.06 |
---|---|
백준 12018번 Yonsei TOTO C++ (1) | 2022.12.05 |
백준 21772번 가희의 고구마 먹방 C++ (0) | 2022.12.03 |
백준 16139번 인간-컴퓨터 상호작용 C++ (0) | 2022.12.02 |
백준 17390번 이건 꼭 풀어야 해! C++ (0) | 2022.11.30 |