【问题标题】:Find if it's possible to make up a integer with some given numbers找出是否可以用一些给定的数字组成一个整数
【发布时间】:2013-11-17 20:14:57
【问题描述】:

问题:

是否可以用 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 - 94 = 19*3 + 37

我的查询:

  1. 你能帮我完成这个任务,展示 DP / Div & Con / Greedy 的路径吗?
  2. 我应该选择哪一个,为什么不选择其他(在这种情况下)?
  3. 如果您能更灵活一点地解释 DP / Greedy / Div & Con 方程并解释您的思维过程,我将不胜感激。
    例如,在最长公共子序列中,我们使用以下内容:

    //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]
    }
    

【问题讨论】:

  • 我不明白这个问题。 19 + 19 - 37 = 1。您可以重复该模式以生成您想要的每个数字 (N = 2 * N * 19 - N * 37)。你能明确允许的操作吗?
  • @Heuster 减法似乎不允许(我不确定“重复”和“替换”在这里应该是什么意思)。

标签: algorithm dynamic-programming greedy


【解决方案1】:

与背包问题相同,参数如下:-

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解:-

Knapsack Problem

注意:你应该自己研究背包问题,不要为它的代码发布另一个问题。

【讨论】:

  • 谢谢维克拉姆,喜欢它....等待任何其他不同的思考过程:)
  • @ArnabDutta 另一种方法是线性整数规划,其中 19*x + 27*y + 37*z = N 和 x,y,z>=0 。可以使用标准求解器求解。但无论如何你都会得到相似的时间复杂度,因为问题是 NP Complete
  • 感谢您的第二个想法...:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-23
  • 1970-01-01
  • 2020-03-11
  • 1970-01-01
  • 2020-06-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多