-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution_1808_지희의고장난계산기_유승아.java
125 lines (99 loc) · 2.62 KB
/
Solution_1808_지희의고장난계산기_유승아.java
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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
//SWEA :: 1808 지희의 고장난 계산기
//2019-03-11
public class Solution_1808_지희의고장난계산기_유승아 {
private static int[] digit;
private static int X, min, push;
private static ArrayList<Integer> list;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int tc = 1; tc <= T; tc++) {
digit = new int[10];
list = new ArrayList<Integer>();
set = new ArrayList<Integer>();
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < 10; i++) {
digit[i] = Integer.parseInt(st.nextToken());
}
String tmp = br.readLine();
X = Integer.parseInt(tmp);
//각각의 숫자를 직접 누를 수 있으면 숫자 개수 만큼 출력하고 continue;
boolean tmpflag = true;
for (int i = 0; i < tmp.length(); i++) {
if (digit[tmp.charAt(i) - '0'] == 0) {
tmpflag = false;
break;
}
}
if (tmpflag) {
System.out.println("#" + tc + " " + (int) (tmp.length() + 1));
continue;
}
//약수구하기
for (int i = 1; i <= Math.sqrt(X); i++) {
if (X % i != 0)
continue;
int val = i;
boolean flag = true;
while (val > 0) {
int r = val % 10; // 1 의 자리 숫자
if (digit[r] == 0) {
flag = false;
break;
}
val /= 10;
}
if (flag && i != 1)
list.add(i);
val = X / i;
flag = true;
while (val > 0) {
int r = val % 10; // 1 의 자리 숫자
if (digit[r] == 0) {
flag = false;
break;
}
val /= 10;
}
if (flag)
list.add(X / i);
}
min = Integer.MAX_VALUE;
solve(0, 0);
System.out.println("#" + tc + " " + ((min < Integer.MAX_VALUE) ? min : -1));
} // end of tc
} // end of main
public static ArrayList<Integer> set;
public static void solve(int len, int k) {
// System.out.println(set);
push = 0;
set.sort(null);
cal(X);
if (len == list.size()) {
return;
}
for (int i = k; i < list.size(); i++) {
set.add(list.get(i));
solve(len + 1, i + 1);
set.remove(set.size() - 1);
}
}
public static void cal(int val) {
if (val == 1) {
min = min > push ? push : min;
return;
}
for (int i = set.size() - 1; i >= 0; i--) {
int y = set.get(i);
if (val % y == 0) {
val /= y;
push += (Integer.toString(y).length() + 1);
cal(val);
}
}
} // end of cal
}