C语言题。分油问题。

Python025

C语言题。分油问题。,第1张

这个简单,程序如下:

****我是注释**************************

*****(qun:37378637)********

程序开始:用C语言实现

#include <stdio.h>

int i

main()

{

int a,y,z

printf("Input Full a,Empty b,c,Get i:")

scanf("%d%d%d%d",&a,&y,&z,&i)

getyou(a,y,z)

while(1)

getchar()

}

getyou(int a,int y,int z)

{

int b=0,c=0

printf("%4d%4d%4d\n%4d%4d%4d\n",a,y,z,a,b,c)

while(a!=i||b!=i)

{

if(!b)

{a-=yb=y}

else if(c==z)

{a+=zc=0}

else if(b>z-c)

{b-=(z-c)c=z}

else

{c+=b

b=0}

printf("%4d%4d%4d\n",a,b,c)

}

}

能不能把问题贴出来啊?

我做了一道ZOJ1005题,差不多吧

In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle.

You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.

A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps are

fill A

fill B

empty A

empty B

pour A B

pour B A

success

where "pour A B" means "pour the contents of jug A into jug B", and "success" means that the goal has been accomplished.

You may assume that the input you are given does have a solution.

Input

Input to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 <Ca <= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another.

Output

Output from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line "success". Output lines start in column 1 and there should be no empty lines nor any trailing spaces.

Sample Input

3 5 4

5 7 3

Sample Output

fill B

pour B A

empty A

pour B A

fill B

pour B A

success

fill A

pour A B

fill A

pour A B

empty B

pour A B

success

代码如下:(当然不是最优解,只是一个可行解而已)

#include<iostream>

#include<stack>

using namespace std

#define N 1001

boolA[N][N]

int X,Y

int M

stack<int>zhan

void Init()

{

int i,j

for(i=0i<Ni++)

for(j=0j<Nj++)

A[i][j]=true

}

bool Xun_zhao(int i,int j)

{

A[i][j]=false

if(i==M||j==M)

{

zhan.push(6)

return true

}

for(int k=0k<6k++)

{

switch(k)

{

case 0:

{

if(i<X&&A[X][j]==true)

{

if(Xun_zhao(X,j))

{

zhan.push(0)

return true

}

else

{

A[X][j]=true

}

}

break

}

case 1:

{

if(j<Y&&A[i][Y]==true)

{

if(Xun_zhao(i,Y))

{

zhan.push(1)

return true

}

else

{

A[i][Y]=true

}

}

break

}

case 2:

{

if(i>0&&A[0][j]==true)

{

if(Xun_zhao(0,j))

{

zhan.push(2)

return true

}

else

{

A[0][j]=true

}

}

break

}

case 3:

{

if(j>0&&A[i][0]==true)

{

if(Xun_zhao(i,0))

{

zhan.push(3)

return true

}

else

{

A[i][0]=true

}

}

break

}

case 4:

{

if(i>0)

{

if(i+j<Y&&A[0][i+j]==true)

{

if(Xun_zhao(0,i+j))

{

zhan.push(4)

return true

}

else

{

A[0][i+j]=true

}

}

else

if(i+j>Y&&A[i-Y+j][Y]==true)

{

if(Xun_zhao(i-Y+j,Y))

{

zhan.push(4)

return true

}

else

{

A[i-Y+j][Y]=true

}

}

}

break

}

case 5:

{

if(j>0)

{

if(i+j<X&&A[i+j][0]==true)

{

if(Xun_zhao(i+j,0))

{

zhan.push(5)

return true

}

else

{

A[i+j][0]=true

}

}

else

if(i+j>X&&A[X][j-X+i]==true)

{

if(Xun_zhao(X,j-X+i))

{

zhan.push(5)

return true

}

else

{

A[X][j-X+i]=true

}

}

}

break

}

}

}

return false

}

int main()

{

while(cin>>X>>Y>>M)

{

Init()

if(Xun_zhao(0,0))

{

while(!zhan.empty())

{

int i=zhan.top()

zhan.pop()

switch(i)

{

case 0:cout<<"fill A"<<endlbreak

case 1:cout<<"fill B"<<endlbreak

case 2:cout<<"empty A"<<endlbreak

case 3:cout<<"empty B"<<endlbreak

case 4:cout<<"pour A B"<<endlbreak

case 5:cout<<"pour B A"<<endlbreak

case 6:cout<<"success"<<endlbreak

}

}

}

}

return 0

}