设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 3805|回复: 0
打印 上一主题 下一主题

【每周一坑】鸡兔同笼

[复制链接]

3

主题

0

好友

37

积分

新手上路

Rank: 1

跳转到指定楼层
楼主
发表于 2018-7-27 15:36:20 |只看该作者 |倒序浏览
  1. from sympy import symbols, solve
  2. from math import modf

  3. def isInt(inp):
  4.     while True:
  5.         try:
  6.             inp = float(inp)
  7.             dec = modf(inp)
  8.             if dec[0] == 0:
  9.                 return inp
  10.                 break
  11.             else:
  12.                 inp = input("Input Error! Try Again: ")
  13.         except:
  14.             inp = input("Input Error! Try again: ")

  15. head = input("Input heads: ")
  16. head = isInt(head)
  17. foot = input("Input foot: ")
  18. foot = isInt(foot)

  19. result = {}
  20. x = symbols('x')
  21. y = symbols('y')
  22. result = solve([x + y - head, 2 * x + 4 * y - foot], [x, y])
  23. print(result)
  24. if result[x] >= 0 and result[y] >= 0 and modf(result[x])[0] + modf(result[y])[0] == 0:
  25.     print("Chicken: %d, rabbits: %d."%(result[x], result[y]))
  26. else:
  27.     print("Unsolvable!")
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-4-20 04:30 , Processed in 0.015437 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部