2016-12-02 47 views
-2

我一直在看下面请转换我的伪代码的工作Python程序

给出的问题给定一组S = {X1,X2,。 。 。 ,xn},整数t(称为目标)决定是否存在一个S的子集,其总和等于t。

请问任何人,我的伪代码转换为一个可用的Python程序?

If L is a set of integers and x is another integer, then we use the shorthand L + x to denote the list of integers derived from L by increasing each element of L by x, i.e. L + x = {`k + x | `k ∈ L}. For example, if L = {1, 5, 9}, then L + 3 = {4, 8, 12}. 

下面是SSP伪代码是基于动态规划方法

1: L ← {0} 
2: for x ∈ {x1, . . . , xn} do 
3:  L ← L ∪ (L + x) 
4:  remove from L every element that is greater than t 
5:  end for 
6:  return True if t ∈ L, False otherwise 
+4

欢迎来到StackOverflow。请阅读并遵守帮助文档中的发布准则。 [最小,完整,可验证的示例](http://stackoverflow.com/help/mcve)适用于此处。在您发布代码并准确描述问题之前,我们无法有效帮助您。 StackOverflow不是一个编码或教程服务。 – Prune

回答

0

首先,不,我们不能变成工作Python代码为你这一点。那是作弊。

代数方面,结果要容易得多。目标可以实现iff

  1. 目标被x整除;
  2. 目标< =总和(S)

总和(S)= X *(N + 1)* n/2的

你的代码实际上构建的溶液;这不是必需的。