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?

7th Aug 2017, 3:21 PM
ⓢⓐⓝⓓⓔⓔⓟ
ⓢⓐⓝⓓⓔⓔⓟ - avatar
7 Respuestas
0
and no exist soluyion if legs>heads*4 or legs <heads*2
4th Sep 2017, 5:34 AM
Guilherme Ferreira Carvalho
Guilherme Ferreira Carvalho - avatar
0
My output is no solution
23rd Sep 2019, 3:26 PM
shreyank gawali
shreyank gawali - avatar
- 1
Here's my code as understandable as possible. https://code.sololearn.com/cfGIyjq8tEkP/?ref=app
7th Aug 2017, 8:25 PM
Kuromaru くろまる
Kuromaru くろまる - avatar
- 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)
8th Aug 2017, 7:09 AM
VcC
VcC - avatar
- 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")
13th Aug 2017, 4:34 PM
Rafael Dias da Silva
Rafael Dias da Silva - avatar
- 1
94/2-35 => rabbits 35-rabbits => chickens
4th Sep 2017, 5:32 AM
Guilherme Ferreira Carvalho
Guilherme Ferreira Carvalho - avatar
- 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)
7th Sep 2020, 4:58 PM
Ñîtíñ Pâñdéy
Ñîtíñ Pâñdéy - avatar