0
Write a program to solve a classic ancient Chinese puzzle:
Write a program to solve a classic ancient Chinese puzzle: We count 35 heads and 94 legs among the chickens and rabbits in a farm. How many rabbits and how many chickens do we have?
7 odpowiedzi
0
and no exist soluyion if legs>heads*4 or legs <heads*2
0
My output is no solution
- 1
Here's my code as understandable as possible.
https://code.sololearn.com/cfGIyjq8tEkP/?ref=app
- 1
# Solving An+Bm=P with integers
def egcd(a, b):
x, y, u, v = 0,1,1,0
while a != 0:
q,r = b//a, b%a
m,n = x-u*q, y-v*q
b, a, x, y, u, v = a, r, u, v, m, n
return b, x, y
print("\nSolving An+Bm=P with integers")
a= int(input("Number A "))
b= int(input("Number B "))
p= int(input("Number P "))
gcd, x, y = egcd(a, b)
if (p % gcd) ==0:
q=p//gcd
print ("\nSolutions are:")
print (a, "*(", int(x*q), "+",int(b/gcd),"k)-", b, "*(", int(-y*q), "+",int(a/gcd),"k)=", p)
else:
print ("\nThere exist no integer solution because ",p,"is not a multiple of ",gcd)
- 1
rabbits, chickens = 35, 0
while rabbits >= 0:
legs = rabbits * 4 + chickens * 2
if legs == 92:
print ("Rabbits : {} \n Chickens : {}".format (rabbits, chickens ))
else:
rabbits, chickens += -1, 1
else:
print ("There is no integer solution for this problem")
- 1
94/2-35 => rabbits
35-rabbits => chickens
- 1
h=int(input("Enter the Number of heads"))
l=int(input("Enter the Number of legs"))
r=int(0)
c=int(0)
if(l%2!=0):
print("NO SOLUTION")
else:
for i in range(h+1):
j=h-i
if (2*i)+(4*j)==l:
print("The number of chicken=",i)
print("The number of rabbit=",j)