Repository navigation
Expand file tree
/
Copy pathboj_1107.java
More file actions
126 lines (120 loc) · 2.67 KB
/
Copy pathboj_1107.java
File metadata and controls
126 lines (120 loc) · 2.67 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package practice;
import java.util.*;
public class Main5 {
static int n, a[], target, min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE, min3 = Integer.MAX_VALUE, length;
static String min_s1 = "-1", min_s2 = "-1", min_s3 = "-1";
static boolean ch[];
public static void f1(int dep) {
if (dep == length) {
String s = "";
for (int i = 0; i < length; i++) {
s += a[i];
}
if (s.equals("")) {
return;
}
int val = Math.abs(Integer.parseInt(s) - target) + s.length();
if (min1 > val) {
min1 = val;
min_s1 = s;
int temp = Integer.parseInt(min_s1);
min_s1 = Integer.toString(temp);
}
return;
}
for (int i = 0; i <= 9; i++) {
if (ch[i]) {
continue;
}
a[dep] = i;
f1(dep + 1);
}
}
public static void f2(int dep) {
if (dep == length - 1) {
String s = "";
for (int i = 0; i < length - 1; i++) {
s += a[i];
}
if (s.equals("")) {
return;
}
int val = Math.abs(Integer.parseInt(s) - target) + s.length();
if (min2 > val) {
min2 = val;
min_s2 = s;
int temp = Integer.parseInt(min_s2);
min_s2 = Integer.toString(temp);
}
return;
}
for (int i = 0; i <= 9; i++) {
if (ch[i]) {
continue;
}
a[dep] = i;
f2(dep + 1);
}
}
public static void f3(int dep) {
if (dep == length + 1) {
String s = "";
for (int i = 0; i < length + 1; i++) {
s += a[i];
}
if (s.equals("")) {
return;
}
int val = Math.abs(Integer.parseInt(s) - target) + s.length();
if (min3 > val) {
min3 = val;
min_s3 = s;
int temp = Integer.parseInt(min_s3);
min_s3 = Integer.toString(temp);
}
return;
}
for (int i = 0; i <= 9; i++) {
if (ch[i]) {
continue;
}
a[dep] = i;
f3(dep + 1);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
target = sc.nextInt();
length = Integer.toString(target).length();
n = sc.nextInt();
ch = new boolean[10];
a = new int[length + 1];
for (int i = 0; i < n; i++) {
ch[sc.nextInt()] = true;
}
int result1 = Math.abs(target - 100);
f1(0);
f2(0);
f3(0);
int temp1;
int temp2;
int temp3;
if (!min_s1.equals("-1")) {
temp1 = Math.abs(Integer.parseInt(min_s1) - target) + min_s1.length();
} else {
temp1 = Integer.MAX_VALUE;
}
if (!min_s2.equals("-1")) {
temp2 = Math.abs(Integer.parseInt(min_s2) - target) + min_s2.length();
} else {
temp2 = Integer.MAX_VALUE;
}
if (!min_s3.equals("-1")) {
temp3 = Math.abs(Integer.parseInt(min_s3) - target) + min_s3.length();
} else {
temp3 = Integer.MAX_VALUE;
}
int result2 = Math.min(Math.min(temp1, temp2), temp3);
System.out.println(Math.min(result1, result2));
}
}