爪哇610
class X{int n,c=0,t=0;public void static main(String[]a){n=Integer.parseInt(a[0]);String s,b,f,k,m,u;b="5L";s="3L";k="tank";u="Fill %s jug\n5L: %d, 3L: %d, T: %d";m="\nPour from %s jug into %s\n5L: %d, 3L: %d, T: %d";f=u+m;for(;n>4;)z(f,2,5,b,5,0,t,b,k,0,0,t+=5);while(n!=0){if(n==1)z(f+f+m,5,1,s,0,3,t,s,b,3,0,t,s,3,3,t,s,b,5,1,t,s,k,5,0,t+1);if(n==3)z(f,2,3,s,0,3,t,s,k,0,0,t+3);z(f+m,3,2,b,5,0,t,b,s,2,3,t,b,k,0,3,t+=2);if(n==2)z("Empty 3L jug\n5L: 0, 3L: 0,T: %d",1,0,t)}z("Volume measured out in %d turns",0,0,c)}void z(String s,int o,int w,Object...a){c+=o;n-=w;System.out.println(String.format(s,a))}}
我拿了Sumedh的溶液并打了高尔夫球。我想在评论中输入它,但我的声誉还不够:(。少了40%,我认为至少应该分享它。不过,距离第一位还很遥远...
这里是无高尔夫球的:
class X{
int n,c=0,t=0;
public void static main(String[] a){
n=Integer.parseInt(a[0]);
String s,b,f,k,m,u;
b="5L";
s="3L";
k="tank";
u="Fill %s jug\n5L: %d, 3L: %d, T: %d";
m="\nPour from %s jug into %s\n5L: %d, 3L: %d, T: %d";
f=u+m;
for(;n>4;)z(f,2,5,b,5,0,t,b,k,0,0,t+=5);
while(n!=0)
{
if(n==1)z(f+f+m,5,1,s,0,3,t,s,b,3,0,t,s,3,3,t,s,b,5,1,t,s,k,5,0,t+1);
if(n==3)z(f,2,3,s,0,3,t,s,k,0,0,t+3);
z(f+m,3,2,b,5,0,t,b,s,2,3,t,b,k,0,3,t+=2);
if(n==2)z("Empty 3L jug\n5L: 0, 3L: 0,T: %d",1,0,t);
}
z("Volume measured out in %d turns",0,0,c);
}
void z(String s,int o, int w,Object... a){
c+=o;
n-=w;
System.out.println(String.format(s,a));
}
}
注意:它仅在第一次运行时有效。重新运行它,结果将是错误的(由于全局变量)。
以下版本是安全的,但我们损失了2个字符,从610变为612:
class X{
int n,c,t;
public void static main(String[] a){
n=Integer.parseInt(a[0]);
String s,b,f,k,m,u;
t=c=0;
b="5L";
s="3L";
k="tank";
u="Fill %s jug\n5L: %d, 3L: %d, T: %d";
m="\nPour from %s jug into %s\n5L: %d, 3L: %d, T: %d";
f=u+m;
for(;n>4;)z(f,2,5,b,5,0,t,b,k,0,0,t+=5);
while(n!=0)
{
if(n==1)z(f+f+m,5,1,s,0,3,t,s,b,3,0,t,s,3,3,t,s,b,5,1,t,s,k,5,0,t+1);
if(n==3)z(f,2,3,s,0,3,t,s,k,0,0,t+3);
z(f+m,3,2,b,5,0,t,b,s,2,3,t,b,k,0,3,t+=2);
if(n==2)z("Empty 3L jug\n5L: 0, 3L: 0,T: %d",1,0,t);
}
z("Volume measured out in %d turns",0,0,c);
}
void z(String s,int o, int w,Object... a){
c+=o;
n-=w;
System.out.println(String.format(s,a));
}
}
N = 69的样本输出:
Fill 5L jug
5L: 5, 3L: 0, T: 0
Pour from 5L jug into tank
5L: 0, 3L: 0, T: 5
Fill 5L jug
5L: 5, 3L: 0, T: 5
Pour from 5L jug into tank
5L: 0, 3L: 0, T: 10
Fill 5L jug
5L: 5, 3L: 0, T: 10
Pour from 5L jug into tank
5L: 0, 3L: 0, T: 15
Fill 5L jug
5L: 5, 3L: 0, T: 15
Pour from 5L jug into tank
5L: 0, 3L: 0, T: 20
Fill 5L jug
5L: 5, 3L: 0, T: 20
Pour from 5L jug into tank
5L: 0, 3L: 0, T: 25
Fill 5L jug
5L: 5, 3L: 0, T: 25
Pour from 5L jug into tank
5L: 0, 3L: 0, T: 30
Fill 5L jug
5L: 5, 3L: 0, T: 30
Pour from 5L jug into tank
5L: 0, 3L: 0, T: 35
Fill 5L jug
5L: 5, 3L: 0, T: 35
Pour from 5L jug into tank
5L: 0, 3L: 0, T: 40
Fill 5L jug
5L: 5, 3L: 0, T: 40
Pour from 5L jug into tank
5L: 0, 3L: 0, T: 45
Fill 5L jug
5L: 5, 3L: 0, T: 45
Pour from 5L jug into tank
5L: 0, 3L: 0, T: 50
Fill 5L jug
5L: 5, 3L: 0, T: 50
Pour from 5L jug into tank
5L: 0, 3L: 0, T: 55
Fill 5L jug
5L: 5, 3L: 0, T: 55
Pour from 5L jug into tank
5L: 0, 3L: 0, T: 60
Fill 5L jug
5L: 5, 3L: 0, T: 60
Pour from 5L jug into tank
5L: 0, 3L: 0, T: 65
Fill 5L jug
5L: 5, 3L: 0, T: 65
Pour from 5L jug into 3L
5L: 2, 3L: 3, T: 65
Pour from 5L jug into tank
5L: 0, 3L: 3, T: 67
Empty 3L jug
5L: 0, 3L: 0,T: 67
Fill 5L jug
5L: 5, 3L: 0, T: 67
Pour from 5L jug into 3L
5L: 2, 3L: 3, T: 67
Pour from 5L jug into tank
5L: 0, 3L: 3, T: 69
Volume measured out in 33 turns