Creative Code

[1107번]리모컨 본문

백준 문제풀이

[1107번]리모컨

빛하루 2023. 9. 24. 17:25

https://www.acmicpc.net/problem/1107

 

1107번: 리모컨

첫째 줄에 수빈이가 이동하려고 하는 채널 N (0 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 고장난 버튼의 개수 M (0 ≤ M ≤ 10)이 주어진다. 고장난 버튼이 있는 경우에는 셋째 줄에는 고장난 버튼이

www.acmicpc.net

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

bool arr[11];
int check(int n) {
    if (n == 0) {
        if (arr[0]) return 0;
        else return 1;
    }
    int len = 0;
    while (n > 0) {
        if (arr[n % 10]) return 0;
        n /= 10;
        len++;
    }
    return len;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int x;
        cin >> x;
        arr[x] = true;
    }
    int ans = n - 100;
    if (ans < 0) ans = -ans;

    for (int i = 0; i <= 10000000; i++) {
        int c = i;
        int len = check(c);
        if (len > 0) {
            int press = abs(c - n);
            if (ans > press + len) {
                ans = press + len;
            }
        }
    }
    cout << ans;
}

'백준 문제풀이' 카테고리의 다른 글

[2166번]다각형의 면적  (0) 2023.09.24
[2096번]내려가기  (0) 2023.09.24
[17214번]다항 함수의 적분  (0) 2023.09.06
[16496번]큰 수 만들기  (0) 2023.09.06
[7869번]두 원  (0) 2023.09.05