강의로 돌아가기

### 아무리 해도 42.9입니다. 도움이 필요합니다.

테스트 1,2,5,7 오류납니다...
조언을 구합니다.. 감사합니다.

###### 작성중인 코드―Solution.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
import java.util.ArrayList;
class Solution {
public int solution(int N, int number) {
ArrayList<Integer> a = new ArrayList<Integer>();
ArrayList<Integer> b = new ArrayList<Integer>();

int temp = 0;
String strtemp = "";
int cnt = 0;
do {
strtemp = strtemp + "1";
temp = Integer.parseInt(strtemp);
cnt++;
} while (temp < 32000 && temp < number);

temp = 0;
strtemp = "";
cnt = 0;
do {
strtemp = strtemp + N + "";
temp = Integer.parseInt(strtemp);
cnt++;
} while (temp < 32000 && temp < number);

temp = 1;
cnt = 0;
do {
temp = temp * N;
cnt++;
} while (temp < 32000 && temp < number);

for (int i = 0; i < a.size(); i++) {
for (int j = i; j < a.size(); j++) {
if (a.get(i) == a.get(j) && i != j) {
if (b.get(i) > b.get(j)) {
a.remove(i);
b.remove(i);
} else {
a.remove(j);
b.remove(j);
}
}
}
}

// 정렬
for (int i = 0; i < a.size(); i++) {
for (int j = i; j < a.size(); j++) {
if (i == j) {

} else {
if (a.get(i) / b.get(i) < a.get(j) / b.get(j)) {
int tempa = a.get(i);
a.set(i, a.get(j));
a.set(j, tempa);
int tempb = b.get(i);
b.set(i, b.get(j));
b.set(j, tempb);
}
}
}
}
int tmp = 0;
for(int i = 0; i< a.size(); i++) {
if(number > a.get(i)) {
tmp = a.get(i-1);
break;
}
}

return Math.min(sol(number, a, b), sol(tmp, a, b) + sol(tmp - number, a, b)) > 8 ? -1 : Math.min(sol(number, a, b), sol(tmp, a, b) + sol(tmp - number, a, b));
}

public static int sol(int number, ArrayList<Integer> a, ArrayList<Integer> b) {
if(number == 0) {
return 0;
}
int division = 0;

for (int i = 0; i < a.size(); i++) {
String tmp = "";
for (int j = 0; j < b.get(i); j++) {
tmp = tmp + "1";
}
if (tmp.length() > 0 && Integer.parseInt(tmp) == a.get(i)) {
int share = number / a.get(i);
if (share > 0) {
division = 1;
}
number = number - (share * a.get(i));
} else {
int share = number / a.get(i);
number = number - (share * a.get(i));
}
break;
}
}