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 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题