Published 2021. 2. 20. 20:09
 

2529번: 부등호

여러분은 제시된 부등호 관계를 만족하는 k+1 자리의 최대, 최소 정수를 첫째 줄과 둘째 줄에 각각 출력해야 한다. 단 아래 예(1)과 같이 첫 자리가 0인 경우도 정수에 포함되어야 한다. 모든 입력

www.acmicpc.net

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int n;
char a[20];
vector<string> ans;
bool check[10];
bool ok(string num) {
    for (int i=0; i<n; i++) {
        if (a[i] == '<') {
            if (num[i] > num[i+1]) return false;
        } else if (a[i] == '>') {
            if (num[i] < num[i+1]) return false;
        }
    }
    return true;
}
void go(int index, string num) {
    if (index == n+1) {
        if (ok(num)) {
            ans.push_back(num);
        }
        return;
    }
    for (int i=0; i<=9; i++) {
        if (check[i]) continue;
        check[i] = true;
        go(index+1, num+to_string(i));
        check[i] = false;
    }
}
int main() {
    cin >> n;
    for (int i=0; i<n; i++) {
        cin >> a[i];
    }
    go(0, "");
    auto p = minmax_element(ans.begin(), ans.end());
    cout << *p.second << '\n';
    cout << *p.first << '\n';
    return 0;
}

 

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int n;
char a[20];
vector<string> ans;
bool check[10];
bool good(char x, char y, char op) {
    if (op == '<') {
        if (x > y) return false;
    }
    if (op == '>') {
        if (x < y) return false;
    }
    return true;
}
void go(int index, string num) {
    if (index == n+1) {
        ans.push_back(num);
        return;
    }
    for (int i=0; i<=9; i++) {
        if (check[i]) continue;
        if (index == 0 || good(num[index-1], i+'0', a[index-1])) {
            check[i] = true;
            go(index+1, num+to_string(i));
            check[i] = false;
        }
    }
}
int main() {
    cin >> n;
    for (int i=0; i<n; i++) {
        cin >> a[i];
    }
    go(0, "");
    auto p = minmax_element(ans.begin(), ans.end());
    cout << *p.second << '\n';
    cout << *p.first << '\n';
    return 0;
}

'Problem set' 카테고리의 다른 글

[백준] 11723 집합  (0) 2021.02.20
[백준] 1248 맞춰봐  (0) 2021.02.20
[백준] 15661 링크와 스타트  (0) 2021.02.20
[백준] 14889 스타트와 링크  (0) 2021.02.20
[백준] 14501 퇴사  (0) 2021.02.20
복사했습니다!