Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第七课百钱百鸡代码问题 #44

Open
Y15096 opened this issue Feb 12, 2023 · 0 comments
Open

第七课百钱百鸡代码问题 #44

Y15096 opened this issue Feb 12, 2023 · 0 comments

Comments

@Y15096
Copy link

Y15096 commented Feb 12, 2023

原题目为公鸡5元一只,母鸡3元一只,小鸡1元三只,用100块钱买一百只鸡,问公鸡、母鸡、小鸡各有多少只。

"""
《百钱百鸡》问题

Version: 0.1
Author: 骆昊
"""

# 假设公鸡的数量为x,x的取值范围是0到20
for x in range(0, 21):
    # 假设母鸡的数量为y,y的取值范围是0到33
    for y in range(0, 34):
        z = 100 - x - y
        if 5 * x + 3 * y + z // 3 == 100 and z % 3 == 0:
            print(f'公鸡: {x}只, 母鸡: {y}只, 小鸡: {z}只')

该代码穷举方案只出现了3种实际可打印的方案不至3种我的代码修改如下

```python
for x in range(1, 21):
    for y in range(1, 34):
        z = 100 - 5*x - 3*y
        if 5*x + y*3 + z == 100 and z > 0:
            print(f'公鸡: {x}只, 母鸡: {y}只, 小鸡: {z*3}只')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant