[USACO]Mother's Milk【JAVA】
来源:互联网 发布:广电网络宽带怎么缴费 编辑:程序博客网 时间:2024/05/20 07:54
[笔者CSDN传送阵]:http://blog.csdn.net/u013247524/article/details/24390101
Farmer John has three milking buckets of capacity A, B, and Cliters. Each of the numbers A, B, and C is an integer from 1through 20, inclusive. Initially, buckets A and B are empty whilebucket C is full of milk. Sometimes, FJ pours milk from one bucketto another until the second bucket is filled or the first bucket isempty. Once begun, a pour must be completed, of course. Beingthrifty, no milk may be tossed out.
Write a program to help FJ determine what amounts of milk he canleave in bucket C when he begins with three buckets as above, poursmilk among the buckets for a while, and then notes that bucket A isempty.
PROGRAMNAME: milk3
INPUTFORMAT
A single line with the three integers A, B, and C.
SAMPLEINPUT (file milk3.in)
8 9 10
OUTPUTFORMAT
A single line with a sorted list of all the possible amounts ofmilk that can be in bucket C when bucket A is empty.
SAMPLEOUTPUT (file milk3.out)
1 2 8 9 10
SAMPLEINPUT (file milk3.in)
2 5 10
SAMPLEOUTPUT (file milk3.out)
5 6 7 8 9 10
-
- 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 set; -
-
public static void main(String[] throwsargs) IOException { -
// Use BufferedReader rather than RandomAccessFile; it's much faster -
BufferedReader f = new BufferedReader( newFileReader( "milk3.in")); -
// input file name goes above -
PrintWriter out = new PrintWriter( newBufferedWriter( newFileWriter( -
"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(); -
-
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( inta, intb, intc){ -
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=>Ca+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); -
else DFS(A, b, c-(A-a)); -
if(c<=B-b)DFS(a,b+c, 0); -
else DFS(a, B, c-(B-b)); -
-
-
} -
} - }
AC结果:
USER: Singles Xin [xl1993a1]TASK: milk3LANG: JAVACompiling...Compile: OKExecuting... 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 yoursubmission #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!
Thanks for your submission!
给的答案为c的,就略过了
- [USACO]Mother's Milk【JAVA】
- [USACO]Mother's Milk【JAVA】
- Mother's Milk
- Mother's Milk(usaco)
- usaco Mother's Milk
- USACO:Mother's Milk
- USACO-Mother's Milk
- USACO Mother's Milk
- USACO Mother's Milk
- USACO Mother's Milk
- USACO: Mother's Milk
- USACO Mother's Milk
- usaco Mother's Milk
- USACO/Mother's Milk (DFS)
- USACO Mother's Milk [ANALYSIS]
- [usaco]1.4 Mother’s Milk
- USACO 1.4 Mother's Milk
- 【USACO】Mother's Milk(搜索)
- Ubuntu 出现apt-get: Packag…
- 利用proftpd在Ubuntu 终端下…
- 2014年3月5日 小记
- [USACO]Prime Cryptarithm【JAVA】
- 将博客搬至CSDN
- [USACO]Mother's Milk【JAVA】
- Hadoop-2.4.0分布式安装手册
- Hadoop中DBInputFormat和DBOutputFormat使用
- java导出excel,自定义列与行
- 《小团队项目管理》第二问 --- 项目经理是救火队长?
- 2014.4.28 解决javax.servlet.ServletException cannot be resolved to a type的问题
- First missing positive
- OC基础—代理模式设计
- 基于C#的SNMP编程(使用SnmpSharpNet)