我有2个输入:
取值{2,9}
目标值= 729
我想找到一个使用这些给定数字的算术运算,它可以产生目标值。我们可以根据需要使用任意数量的任意数字。只允许Sum和Multiplication操作。
例如:9 * 9 * 9 = 729
另外,我们可以使用数字组合。
对于Ex:
取值{2,9}
目标值= 49
解决方案可以是:
2 + 9 + 9 + 29 = 49
我想使用C ++来使用Dynamic Programmming approch。
答案 0 :(得分:0)
思考这个算法。
1. make all numbers using that digit set. {2, 9} target=729 n = digit set numers -make numbers that mixed by the digits lower than target, and duplicable. digit length1 ; n count ; 2, 9 digit length2 ; n*n count ; 22, 29, 92, 99 digit length3 ; n*n*n count ; 222, 229, 292, 299, 922, 929, 992, 999 (remove bigger than 729) digit length 4 ; no more find. target length is 3. -make new number set by upper found numbers 2, 9, 22, 29, 92, 99 222, 229, 292, 299 m = # of upper set. o = 2 (# of operators) -and operation case... use operator plus and multiply. 2. make operation set. use 1 node ; check the upper elements is equal to target m cases use 2 node ; pick 2 elements. and select operator. 1 operator use. m*m * o cases use 3 node ; pick 3 elements. and select operator. 2 operator use. m*m*m * o*o cases and so on. but how many times doing this? in worse case, if the number set has 1. 1+1+1+...=target(729 times) And this problem... 1*1*1*... infinite case... 1*9*9*9, 1*1*1*9*9*9, 1*...*1*9*9*9, ...
需要一些限制。并且需要更有效的算法。