背景需求:
全民等羊的日子里,微信的段子里有一道源自鸡兔同笼的题目——“阴阳同屋”
问题:屋里共有15人,抗原红线共计23条,阴性和阳性人数个有多少?
运用Python解答这个问题。
解法1:for 遍历人数
print('题目:阴阳同屋,屋里共有15人,抗原红线共计23条,阴性和阳性人数个有多少?')
people=int(input('屋里一共有几个人(15)?\n'))
antigen=int(input('抗原红线一共几条红线(23)\n'))# antigen 抗原
for negaive in range(1,people+1): # negaive:阴性
positive = people - negaive # positive:阳性性
if negaive*1+positive*2==antigen:
print('一共有',negaive,'人阴性')
print('一共有',positive,'人阳性')
说明:鸡(*2)兔(*4)同笼,如果“脚”不是偶数,会出现“无解”,但是阴(*1)阳(*2)同屋,由于阴为1,如果抗原红线数量比人数多1,是偶数或奇数,都可以算出阴阳。
解法2:for 遍历人数
print('题目:阴阳同屋,屋里共有15人,抗原红线共计23条,阴性和阳性人数个有多少?')
people=int(input('屋里一共有几个人(15)?\n'))
antigen=int(input('抗原红线一共几条红线(23)\n'))# antigen 抗原
for negaive in range(0,people):
for positive in range(0,people):
if negaive+positive==people and negaive*1+positive*2==antigen:
print('一共有',negaive,'人阴性')
print('一共有',positive,'人阳性')
其他鸡兔同笼的python写法
Python学习23:鸡兔同笼(Python123)_python鸡兔同笼-CSDN博客文章浏览阅读9.6k次,点赞6次,收藏29次。描述 大约在1500年前,《孙子算经》中记载一个有趣的问题:今有雉兔同笼,上有三十五头,下有九十四足,问雉兔各几何?如无解则输出“Data Error!_python鸡兔同笼https://blog.csdn.net/weixin_60530224/article/details/130669571?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170634293616800213042831%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=170634293616800213042831&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_click~default-4-130669571-null-null.142^v99^pc_search_result_base3&utm_term=%E9%B8%A1%E5%85%94%E5%90%8C%E7%AC%BCpython&spm=1018.2226.3001.4187用python解决鸡兔同笼的问题_python鸡兔同笼-CSDN博客文章浏览阅读2w次,点赞8次,收藏40次。用python解决鸡兔同笼_python鸡兔同笼
https://blog.csdn.net/hu20100913/article/details/126545529?ops_request_misc=&request_id=&biz_id=102&utm_term=%E9%B8%A1%E5%85%94%E5%90%8C%E7%AC%BCpython&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-5-126545529.nonecase&spm=1018.2226.3001.4187