File tree Expand file tree Collapse file tree 1 file changed +56
-0
lines changed
Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
7+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
8+ static StringTokenizer st;
9+ static int N , M ;
10+ static boolean [] broken = new boolean [10 ];
11+
12+ public static void main (String [] args ) throws IOException {
13+ N = Integer . parseInt(br. readLine());
14+ M = Integer . parseInt(br. readLine());
15+ if (M != 0 ){
16+ st = new StringTokenizer (br. readLine());
17+ for (int i = 0 ; i < M ; i++ ) {
18+ broken[Integer . parseInt(st. nextToken())] = true ;
19+ }
20+ }
21+
22+ int result = Math . abs(N - 100 );
23+
24+ for (int channel = 0 ; channel <= 1000000 ; channel++ ) {
25+ int cnt = getCount(channel);
26+ if (cnt > 0 ) {
27+ int total = cnt + Math . abs(N - channel);
28+ result = Math . min(result, total);
29+ }
30+ }
31+
32+ bw. write(result+ " " );
33+ bw. close();
34+ }
35+
36+ static int getCount (int channel ) {
37+ if (channel == 0 ) {
38+ return broken[0 ] ? - 1 : 1 ;
39+ }
40+
41+ int count = 0 ;
42+ int temp = channel;
43+
44+ while (temp > 0 ) {
45+ int digit = temp % 10 ;
46+ if (broken[digit]) {
47+ return - 1 ;
48+ }
49+ count++ ;
50+ temp /= 10 ;
51+ }
52+
53+ return count;
54+ }
55+ }
56+ ```
You can’t perform that action at this time.
0 commit comments