Algorithm 查找是否为';It’用一些给定的数字组成一个整数是可能的
问题: 找出是否可以用21、19和37组成一个整数(比如N)Algorithm 查找是否为';It’用一些给定的数字组成一个整数是可能的,algorithm,dynamic-programming,greedy,Algorithm,Dynamic Programming,Greedy,问题: 找出是否可以用21、19和37组成一个整数(比如N) a. N will be provided as input b. You can use only these three numbers: 27,19,37 c. Only multiplication, addition, repetition and replacement are allowed 例如: Input: 24, Output: not possible Input: 94, Output: possible
a. N will be provided as input
b. You can use only these three numbers: 27,19,37
c. Only multiplication, addition, repetition and replacement are allowed
例如:
Input: 24, Output: not possible
Input: 94, Output: possible - 94 = 19*3 + 37
我的问题是:
例如,在最长公共子序列中,我们使用以下内容:
//assuming X[i.....m] and Y[j.....n]
LCS(i,j) = {
0 , when i = m or j=n
Max { LCS(i, j+1) , LCS(i+1, j) } when X[i] ≠ Y[j]
1+ LCS(i+1,j+1) when X[i] == Y[j]
}
与背包问题相同,参数如下:-
W = Knapsack capacity = N
items = 19 ( N/19 times), 27 (N/27 times), 37 (N/37 times).
Cost & weight of items are same.
Maximize profit. If maximum profit equals N then it is possible to construct N using 19,27,37
背包问题有一个DP解:-
注意:你应该自己研究背包问题,不要再为背包的代码发帖了。我不明白这个问题<代码>19+19-37=1。您可以重复该模式来生成所需的每个数字(
N=2*N*19-N*37
)。你能把允许的运算明确化吗?@Heuster减法似乎是不允许的(我不确定这里的“重复”和“替换”是什么意思)。谢谢Vikram,喜欢它…也在等待其他不同的思维过程:)@ArnabDutta另一种方法是线性整数规划,其中19*x+27*y+37*z=N,x,y,z>=0。可以使用标准解算器求解。但在任何情况下,你都会得到类似的时间复杂度,因为问题是NP完全问题(第二个想法是……)