-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1107.cpp
More file actions
47 lines (40 loc) · 1.15 KB
/
1107.cpp
File metadata and controls
47 lines (40 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
//처음에 N사이즈 그냥 줬다가 처음 구하려는것의 길이를 누르는게 아니라 내가 찾는 숫자를 누르는거니까 틀림.
//두번째로 N * 2로 줬다 틀림 왜냐면 2배 이상 3배 4배 위로 갔다가 내려오는게 더 빠를 수도 있거든. 반례 1 9 0 1 2 3 4 5 6 7 8
#include <bits/stdc++.h>
#define SETTING ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define VMAX 1000000
using namespace std ;
bool arr[10] ;
int N, K ;
bool check(int i)
{
string str = to_string(i) ;
for(int i = 0 ; i < str.length() ; i++)
if(arr[str[i] - '0']) return false ;
return true ;
}
int main()
{
SETTING ;
cin >> N >> K ;
while(K--)
{
int broken ;
cin >> broken ;
arr[broken] = true ;
}
int NSIZE = to_string(N).length() ;
int answer = abs(N - 100) ;
if(K == 10) cout << answer ;
else if(K == 0) cout << NSIZE ;
else
{
for(int i = 0 ; i <= VMAX ; i++)
{
if(!check(i)) continue ;
NSIZE = to_string(i).length() ;
answer = min(answer, abs(N - i) + NSIZE ) ;
}
cout << answer ;
}
}