일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- pytorch
- dropout
- Overfitting
- 자바스크립트
- 크루스칼
- 너비 우선 탐색
- 백트래킹
- 가끔은_말로
- object detection
- 플로이드 와샬
- 조합론
- back propagation
- 우선 순위 큐
- 다익스트라
- 문자열
- 가끔은 말로
- 분할 정복
- 이분 탐색
- 알고리즘
- dfs
- 미래는_현재와_과거로
- DP
- 세그먼트 트리
- tensorflow
- lazy propagation
- NEXT
- BFS
- c++
- 2023
- 회고록
Archives
- Today
- Total
Doby's Lab
백준 2992번: 크면서 작은 수 (C++) 본문
https://www.acmicpc.net/problem/2992
Solved By: Back Tracking, String to Int (stoi)
가능한 경우의 수를 찾기 위해 백트래킹을 사용하였습니다. 문자열 비교가 되지 않길래 stoi 함수를 통해 정수형으로 변환하여 compare 하여 문제에서 요구하는 s보다 크면서 제일 작은 수를 찾을 수 있었습니다.
#include <iostream>
#include <string>
using namespace std;
string s;
int ans = 1000000;
bool visited[7];
void backTracking(string temp, int cnt){
if(cnt == s.size()){
if(temp > s){
int tempInt = stoi(temp);
ans = tempInt < ans ? tempInt : ans;
}
return;
}
for(int i = 0; i < s.size(); i++){
if(!visited[i]){
visited[i] = true;
backTracking(temp + s[i], cnt + 1);
visited[i] = false;
}
}
}
int main(){
cin >> s;
backTracking("", 0);
if(ans == 1000000) cout << 0;
else cout << ans;
return 0;
}
728x90
'PS > BOJ' 카테고리의 다른 글
백준 6850번: Cows (C++) (0) | 2022.04.17 |
---|---|
백준 10903번: Wall construction (C++) (0) | 2022.04.16 |
백준 1940번: 주몽 (C++) (0) | 2022.04.11 |
백준 2191번: 들쥐의 탈출 (C++) (0) | 2022.03.19 |
백준 2188번: 축사 배정 (C++) (0) | 2022.03.19 |