鱼C论坛

 找回密码
 立即注册
查看: 1867|回复: 5

[已解决]Python鸡兔同笼的输出格式问题

[复制链接]
发表于 2021-10-13 21:47:30 | 显示全部楼层 |阅读模式
25鱼币
来自python初学者的求助,这是一个鸡兔同笼的问题,我的算法和答案应该都没有问题,问题出在输出格式上,题目见下图

题目

题目

以下是我写的代码

  1. counts = int(input())
  2. res = []
  3. while counts > 0:
  4.     head = int(input())
  5.     feet = int(input())
  6.     feet1 = head * 4
  7.     feet2 = feet1 - feet
  8.     head1 = int(feet2 / 2)
  9.     head2 = int(head - head1)
  10.     if head1 % 1 == 0 and head2 % 1 == 0 and head1 >= 0 and head2 >= 0:
  11.         temp1 = str(head1)
  12.         temp2 = str(head2)
  13.         res.append(temp1)
  14.         res.append(temp2)
  15.     else:
  16.         x = "No answer"
  17.         res.append(x)
  18.     counts -= 1
  19. for i in res:
  20.     print(i, end=" ")
复制代码



学校oj网站的要求是必须在输入结束后一次性将所有计算结果一次性输出出来,但是我写的代码需要需要一次一次的把每组结果print出来,请问各位大佬,应该如何修改代码,才能实现正确的输出格式?马上就要参加新生赛了,萌新跪谢!!!
最佳答案
2021-10-13 21:47:31
看不见lz的图,但是把lz的代码运行了一下,不怎么好理解,改了一下:
  1. counts = int(input('項目組數'))
  2. res = []
  3. x = 1
  4. while x <= counts:
  5.     print('第',x,'組項目')
  6.     head = int(input('頭'))
  7.     feet = int(input('腳'))
  8.     feet1 = head * 4
  9.     feet2 = feet1 - feet
  10.     head1 = int(feet2 / 2)
  11.     head2 = int(head - head1)
  12.     if head1 % 1 == 0 and head2 % 1 == 0 and head1 >= 0 and head2 >= 0:
  13.         temp = '第' + str(x) + '組,雞:' + str(head1) + ',兔:' + str(head2)
  14.         res.append(temp)
  15.     else:
  16.         x = "No answer"
  17.         res.append(x)
  18.     x += 1
  19.    
  20. for i in res:
  21.     print(i)
复制代码



运行结果:
  1. 項目組數3
  2. 第 1 組項目
  3. 頭3
  4. 腳8
  5. 第 2 組項目
  6. 頭3
  7. 腳10
  8. 第 3 組項目
  9. 頭3
  10. 腳12
  11. 第1組,雞:2,兔:1
  12. 第2組,雞:1,兔:2
  13. 第3組,雞:0,兔:3
复制代码




最佳答案

查看完整内容

看不见lz的图,但是把lz的代码运行了一下,不怎么好理解,改了一下: 运行结果:
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-13 21:47:31 | 显示全部楼层    本楼为最佳答案   
看不见lz的图,但是把lz的代码运行了一下,不怎么好理解,改了一下:
  1. counts = int(input('項目組數'))
  2. res = []
  3. x = 1
  4. while x <= counts:
  5.     print('第',x,'組項目')
  6.     head = int(input('頭'))
  7.     feet = int(input('腳'))
  8.     feet1 = head * 4
  9.     feet2 = feet1 - feet
  10.     head1 = int(feet2 / 2)
  11.     head2 = int(head - head1)
  12.     if head1 % 1 == 0 and head2 % 1 == 0 and head1 >= 0 and head2 >= 0:
  13.         temp = '第' + str(x) + '組,雞:' + str(head1) + ',兔:' + str(head2)
  14.         res.append(temp)
  15.     else:
  16.         x = "No answer"
  17.         res.append(x)
  18.     x += 1
  19.    
  20. for i in res:
  21.     print(i)
复制代码



运行结果:
  1. 項目組數3
  2. 第 1 組項目
  3. 頭3
  4. 腳8
  5. 第 2 組項目
  6. 頭3
  7. 腳10
  8. 第 3 組項目
  9. 頭3
  10. 腳12
  11. 第1組,雞:2,兔:1
  12. 第2組,雞:1,兔:2
  13. 第3組,雞:0,兔:3
复制代码




想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-10-13 21:48:32 | 显示全部楼层
我写的代码修改过,所以可能比较乱,希望大家能帮我看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-13 23:07:26 | 显示全部楼层
counts = []
while True:
    count = input()
    if count == '':
        break
    else:
        counts.append(count)
====
既然是需要提示,那到这里应该足够了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-14 10:32:31 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-14 10:58:51 | 显示全部楼层
本帖最后由 LZRoc 于 2021-10-14 11:21 编辑

抛砖引玉


  1. """鸡兔同笼"""
  2. res = []
  3. number = 1
  4. while True:
  5.     try:
  6.         counts = int(input("请输入项目组数:"))
  7.         if counts > 10 or counts <= 0:
  8.             raise ValueError
  9.         break
  10.     except ValueError:
  11.         print("项目组数控制在10组以内")
  12.         continue
  13. print("输入头数和脚数时,请以空格隔开!")
  14. while number <= counts:
  15.     print("第",number,"组项目:")
  16.     while True:
  17.         try:
  18.             head,feet = map(int,input("请输入头数和脚数:").split())
  19.             if head < 0 or feet <0:
  20.                 raise ValueError
  21.             break
  22.         except ValueError:
  23.             print("请输入两个正整数")
  24.             continue
  25.     headC = (head * 4 - feet)/ 2  #计算鸡的数量
  26.     headR = head - headC  #计算兔的数量
  27.     if headR % 1 == 0 and headC % 1 == 0 and headR >= 0 and headC >= 0:
  28.         temp ="第" + str(number) +  "组项目,鸡:"+ str(int(headC)) + "只,兔:"+ str(int(headR)) + "只"
  29.         res.append(temp)
  30.     else:
  31.         x = "第" + str(number) + "组项目,No answer"
  32.         res.append(x)
  33.     number += 1
  34. for i in res:
  35.     print(i)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 18:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表