Farmer John has three milking buckets of capacity A, B, and C liters. Each of the numbers A, B, and C is an integer from 1 through 20, inclusive. Initially, buckets A and B are empty while bucket C is full of milk. Sometimes, FJ pours milk from one bucket to another until the second bucket is filled or the first bucket is empty. Once begun, a pour must be completed, of course. Being thrifty, no milk may be tossed out.
Write a program to help FJ determine what amounts of milk he can leave in bucket C when he begins with three buckets as above, pours milk among the buckets for a while, and then notes that bucket A is empty.
PROGRAM NAME: milk3
INPUT FORMAT
A single line with the three integers A, B, and C.
SAMPLE INPUT (file milk3.in)
8 9 10
OUTPUT FORMAT
A single line with a sorted list of all the possible amounts of milk that can be in bucket C when bucket A is empty.
SAMPLE OUTPUT (file milk3.out)
1 2 8 9 10
SAMPLE INPUT (file milk3.in)
2 5 10
SAMPLE OUTPUT (file milk3.out)
5 6 7 8 9 10
/* ID: Singles Xin[xl1993a1] LANG: JAVA PROG: milk3 */ import java.io.*; import java.util.StringTokenizer; import java.util.Collection; import java.util.TreeSet; public class milk3 { public static int A; public static int B; public static int C; public static boolean[][][] found; public static Collection<Integer> set; public static void main(String[] args) throws IOException { // Use BufferedReader rather than RandomAccessFile; it's much faster BufferedReader f = new BufferedReader(new FileReader("milk3.in")); // input file name goes above PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter( "milk3.out"))); StringTokenizer st = new StringTokenizer(f.readLine()); A = Integer.parseInt(st.nextToken()); B = Integer.parseInt(st.nextToken()); C = Integer.parseInt(st.nextToken()); found = new boolean[A+1][B+1][C+1]; set = new TreeSet<Integer>(); DFS(0,0,C); int flag = 0; for(int i: set){ if(!(flag == 0)){ out.print(" "); }else{ flag++; } out.print(i); } out.println(); out.close(); // close the output file System.exit(0); // don't omit this! } public static void DFS(int a, int b, int c){ if(!found[a][b][c]){ found[a][b][c] = true; if(a == 0){ set.add(c); } if(a<=B-b)DFS(0,a+b, c); //A=>B else DFS(a-(B-b), B, c); if(a<=C-c)DFS(0, b, a+c); //A=>C else DFS(a-(C-c), b, C); if(b<=A-a)DFS(a+b,0, c); //B=>A else DFS(A, b-(A-a), c); if(b<=C-c)DFS(a,0, c+b); //B=>C else DFS(a, b-(C-c), C); if(c<=A-a)DFS(a+c,b, 0); //C=>A else DFS(A, b, c-(A-a)); if(c<=B-b)DFS(a,b+c, 0); //C=>B else DFS(a, B, c-(B-b)); } } }
AC结果:
Thanks for your submission!USER: Singles Xin [xl1993a1] TASK: milk3 LANG: JAVA Compiling... Compile: OK Executing... Test 1: TEST OK [0.101 secs, 32588 KB] Test 2: TEST OK [0.122 secs, 30540 KB] Test 3: TEST OK [0.115 secs, 32588 KB] Test 4: TEST OK [0.108 secs, 30540 KB] Test 5: TEST OK [0.108 secs, 30540 KB] Test 6: TEST OK [0.115 secs, 30540 KB] Test 7: TEST OK [0.122 secs, 30540 KB] Test 8: TEST OK [0.115 secs, 31564 KB] Test 9: TEST OK [0.101 secs, 30540 KB] Test 10: TEST OK [0.115 secs, 32588 KB] All tests OK.Your program ('milk3') produced all correct answers! This is your submission #4 for this problem. Congratulations!
Here are the test data inputs:
------- test 1 ---- 2 5 10 ------- test 2 ---- 20 20 20 ------- test 3 ---- 5 11 15 ------- test 4 ---- 2 12 20 ------- test 5 ---- 19 4 11 ------- test 6 ---- 5 11 13 ------- test 7 ---- 3 20 20 ------- test 8 ---- 7 16 20 ------- test 9 ---- 20 10 9 ------- test 10 ---- 7 12 18Keep up the good work!
给的答案为c的,就略过了