若干个分数相加和恰好为1

deepen099 [email protected]
发件人当地时间:         发送时间 19:45 (GMT-08:00)。发送地当前时间:上午5:36。 ✆
回复:      [email protected]
主题:      [CPyUG] 关于若干个分数相加和恰好为1的组成方案

问题

HYRY

         HYRY [email protected]
发件人当地时间:         发送时间 20:07 (GMT-08:00)。发送地当前时间:上午5:37。 ✆

试试下面的程序,python自带了排列组合的函数,直接用就好了。

   1 from itertools import chain, combinations
   2 from fractions import Fraction
   3 numbers = [2,3,4,5,6,8,10,12,15]
   4 for n in chain(*(combinations(numbers,i) for i in xrange(len(numbers)))):
   5     n = [Fraction(1, x) for x in n]    
   6     if sum(n) == 1:
   7         print " + ".join(str(x) for x in n)


反馈

创建 by -- ZoomQuiet [2011-11-11 13:38:39]

MiscItems/2011-11-11 (last edited 2011-11-11 13:38:41 by ZoomQuiet)