Skip to content

Instantly share code, notes, and snippets.

@flankerhqd
Created June 15, 2013 08:14
Show Gist options
  • Select an option

  • Save flankerhqd/5787367 to your computer and use it in GitHub Desktop.

Select an option

Save flankerhqd/5787367 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <cstdlib>
char* mapping[255];
int main(int argc, char** argv)
{
int a,b,c,i,n,t,x,y,va,vb;
a = atoi(argv[1]);
b = atoi(argv[2]);
c = atoi(argv[3]);
char aa,bb;
if (a > b )
{
int t = a;
a = b;
b = t;
mapping['A'] = "blue jug";
mapping['B'] = "red jug";
}
else
{
mapping['A'] = "red jug";
mapping['B'] = "blue jug";
}
//while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
x=y=0;
for(n=2;;n++){
for(i=1;i<n;i++){
t=i*a-(n-i)*b;
if(t==c){
x=i;y=n-i;
aa='A';bb='B';
break;
}
if(t==-c){
x=n-i;y=i;
t=a;a=b;b=t;aa='B';bb='A';
break;
}
}
if(x||y) break;
}
va=vb=0;
while(x||y){
if(va==0){
printf("fill %s\n",mapping[aa]);
va=a;
x--;
}
else if(vb==b){
if(--y) printf("empty %s\n",mapping[bb]);
vb=0;
}
else{
printf("pour %s into %s\n",mapping[aa],mapping[bb]);
t=b-vb;
if(t>va) t=va;
va-=t;
vb+=t;
}
}
if(aa!='B')
printf("empty %s\npour %s into %s\n", mapping['B'], mapping['A'], mapping['B']);
}
return 0; /* NZEC */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment