BOJ 2529 : 부등호 - C++
부등호

코드
#include <cstdio>
#include <vector>
#include <queue>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int N;
int arr[10];
bool isused[10];
string c[10];
vector<string> ans;
void func(int depth){
    if(depth == N){
        string tmp = "";
        for(int i=0;i<=N;i++)
            tmp+=to_string(arr[i]);
        ans.push_back(tmp);
    }else{
        for(int i=0;i<=9;i++)
        {
            if(isused[i]) continue;
            string t = c[depth];
            bool flag;
            if(t == "<"){
                flag = arr[depth] < i ? true : false;
            }else{
                flag = arr[depth] > i ? true : false;
            }
            if(!flag) continue;
            isused[i] = true;
            arr[depth+1] = i;
            func(depth+1);
            isused[i] = false;
        }
    }
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> N;
    for(int i=0;i<N;i++)
        cin >> c[i];
    for(int i=0;i<=9;i++)
    {
        isused[i] = true;
        arr[0] = i;
        func(0);
        isused[i] = false;
    }
    sort(ans.begin(), ans.end());
    cout << ans.back() << '\n';
    cout << ans.front() << '\n';
}
- 로직
 : 조건에 맞게백트래킹을 수행하면 된다
 (입력이 10보다 작기때문에 충분히 가능)
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Author And Source
                            
                            이 문제에 관하여(BOJ 2529 : 부등호 - C++), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://velog.io/@neity16/BOJ-2529-부등호-C
                            
                            
                            
                                저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
                            
                            
                                
                                
                                 우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            

#include <cstdio> #include <vector> #include <queue> #include <iostream> #include <cmath> #include <algorithm> using namespace std; int N; int arr[10]; bool isused[10]; string c[10]; vector<string> ans; void func(int depth){ if(depth == N){ string tmp = ""; for(int i=0;i<=N;i++) tmp+=to_string(arr[i]); ans.push_back(tmp); }else{ for(int i=0;i<=9;i++) { if(isused[i]) continue; string t = c[depth]; bool flag; if(t == "<"){ flag = arr[depth] < i ? true : false; }else{ flag = arr[depth] > i ? true : false; } if(!flag) continue; isused[i] = true; arr[depth+1] = i; func(depth+1); isused[i] = false; } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N; for(int i=0;i<N;i++) cin >> c[i]; for(int i=0;i<=9;i++) { isused[i] = true; arr[0] = i; func(0); isused[i] = false; } sort(ans.begin(), ans.end()); cout << ans.back() << '\n'; cout << ans.front() << '\n'; }
- 로직
: 조건에 맞게백트래킹을 수행하면 된다
(입력이 10보다 작기때문에 충분히 가능)
Author And Source
이 문제에 관하여(BOJ 2529 : 부등호 - C++), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@neity16/BOJ-2529-부등호-C저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)