dongzhong3688 2016-01-08 11:29 采纳率: 0%
浏览 50
已采纳

从6个随机数中计算出随机的三位数?

I have 4 years PHP and C# experience, but Math is not my better side. I thnik that i need in this project use some math algorithms.

When page load I need randomly create 7 numbers, 6 are numbers that I can use to calculate given three digit number:

  • rand 1-9
  • rand 1-9
  • rand 1-9
  • rand 1-9
  • rand 10-100 //5 steps
  • rand 10-100 //5 steps

and given number to calculate is 100-999,

I can use this operations: +, -, /, *, (, )

What is best algorithm for this? I probably need to try all possible combinations with this 6 numbers to calculate given number or closest number of calculations.

example: let say that given three digit number is 350, and I need to calculate this number from this numbers: 3,6,9,5 10, 100 so formula for this is: (100*3)+(5*10) = 350

if is not possible to calculate exact number, than calculate closest.

You don't need to solve this problem completely, you can introduce me to solve this problem by paste some pseudo, or describing how to do that.

  • 写回答

2条回答 默认 最新

  • dongxing4805 2016-01-08 12:28
    关注

    I have no actual experience that might help you with this, though since you're asking for some insight, I'll share my thoughts on how to do this.

    As I typed my answer, I realised that this is in fact a knapsack problem, which means you can solve it to optimality using any algorithm that solves the knapsack problem. I recommend using dynamic programming to make your program run faster.

    What you need to do is construct all numbers you can generate by combining two numbers with an operator, so that after this you have a list containing the numbers you started with, and the numbers you generated.

    Then you solve the knapsack problem using the numbers as items with their value as their weight, and the number as the weight you can store at most.

    The only thing that is slightly different is that you have an extra constraint that says that you may only use a number once. So you need to add into your implementation that if you add a combination of numbers, that you must remove the option of storing another combination that is constructed with the same number.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献