forked from nanwan03/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1416 Shredding Company.java
74 lines (73 loc) · 1.78 KB
/
1416 Shredding Company.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
import java.util.*;
public class Main {
private static boolean rejected = false;
private static boolean error = true;
private static int[] rst = new int[8];
private static int[] curCut = new int[8];
private static int rstSum = 0;
private static int curCuttedSum = 0;
private static int rstLen = 0;
private static int curCuttedLen = 0;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (true) {
int target = in.nextInt();
int input = in.nextInt();
if (target == 0 && input == 0) {
System.exit(0);
}
init();
helper(target, input);
if (rejected && !error) {
System.out.println("rejected");
} else if (error) {
System.out.println("error");
} else {
System.out.print(rstSum + " ");
for (int i = rstLen - 1; i >= 0; --i) {
if (i == 0) {
System.out.println(rst[i]);
} else {
System.out.print(rst[i] + " ");
}
}
}
}
}
private static void init() {
rejected = false;
error = true;
rstSum = 0;
curCuttedSum = 0;
rstLen = 0;
curCuttedLen = 0;
Arrays.fill(rst, 0);
Arrays.fill(curCut, 0);
}
private static void helper(int target, int input) {
if (input == 0) {
if (curCuttedSum <= target && rstSum < curCuttedSum) {
rstSum = curCuttedSum;
rstLen = curCuttedLen;
rejected = false;
error = false;
for (int i = 0; i < rstLen; ++i) {
rst[i] = curCut[i];
}
} else if (curCuttedSum <= target && rstSum == curCuttedSum) {
rejected = true;
}
return;
}
if (input + curCuttedSum < rstSum) {
return;
}
for (int degree = 10; degree <= input * 10; degree *= 10) {
curCuttedSum += input % degree;
curCut[curCuttedLen++] = input % degree;
helper(target, input / degree);
--curCuttedLen;
curCuttedSum -= input % degree;
}
}
}