-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
58 lines (51 loc) · 1.15 KB
/
Copy pathmain.cpp
File metadata and controls
58 lines (51 loc) · 1.15 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <cstdio>
#include <cstdlib>
using namespace std;
int buttons[10];
bool ispossible(int x) {
if(x == 0){
if(buttons[0]) return true;
else return false;
}
while(x > 0) {
if(buttons[x%10] == 0)
return false;
x /= 10;
}
return true;
}
int main() {
int n, m, input;
scanf("%d", &n);
scanf("%d", &m);
for(int i=0; i<10; i++){
buttons[i] = 1;
}
for(int i=0; i<m; i++) {
scanf("%d", &input);
buttons[input] = 0;
}
int dist;
int count = 0; // 누른 횟수
// 1. 100 번부터 차례로 하는것과 비교
dist = abs(n-100);
// 2. N과 최대 가능한 수 까지 구한 뒤 1. 과 비교
for(int i=0; i<=1000000; i++) {
int j = i;
int length = 0;
if(ispossible(j)) {
count = abs(n-j);
if(j < 10) length = 1;
else {
while(j) {
length++;
j /= 10;
}
}
if(dist > count + length)
dist = count + length;
}
}
printf("%d\n", dist);
return 0;
}