程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python buy hundreds of chickens for hundreds of money

編輯:Python

One 、 problem

Someone has 100 Yuan , buy 100 chicken . cock 5 One yuan , hen 3 One yuan , Chick 1 Three yuan .
ask : You can buy roosters 、 hen 、 How many chickens each .

Two 、 Problem analysis

Use the exhaustive method , Try every possibility , Get the final correct result .
If the cock is x only , Hen is y only , The chicken is z only ;
We will get the formula : 5x+3y+z/3=100; also
x+y+z=100;
Chicken number divided by 3 The remainder of must be 0

3、 ... and 、 Code

money=100
score=0
for x in range(1,21): # A hundred dollars can buy at most 20 A rooster 
for y in range(34): # A hundred dollars can buy at most 33 A hen 
for z in range(301): # A hundred dollars can buy at most 300 A chicken 
score=5 * x + 3 * y + z / 3
if score==money and x+y+z==100 :
print(' The cock is %s only , The hen is %s only , Chicken is %s only '%(x,y,z))

Four 、 Output results

5、 ... and 、 communication

If you have a better solution or have different opinions , You can say what you want , Jointly improve progress !
I'm also Xiaobai , Dare to ask questions , Let's discuss , Learn together and make progress !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved