Problem with numbers

Posted by StolePopov on Stack Overflow See other posts from Stack Overflow or by StolePopov
Published on 2010-03-20T10:29:31Z Indexed on 2010/03/20 10:31 UTC
Read the original article Hit count: 426

Filed under:
|
|
|

I am given a number N, and i must add some numbers from the array V so that they wil be equal. V is consisting of numbers that are all powers of 3:

    N = 17
    S = 0
V = 1 3 9 27 81 ..

I should add numbers from V to N and S in order to make them equal. The solution to the example above is : 17 + 1 + 9 = 27, 27, 1 and 9 are taken from V, a number from V can be taken only once, and when taken it's removed from V.

I tried sorting V and then adding the biggest numbers from V to S until S has reached N, but it fails on some tests when it's like:

N = 7
S = 0
V = 1 3 9 27
So the solution will be:
7 + 3 = 9 + 1

In examples like this i need to add numbers both to N and S, and also select them so they become equal. Any idea of solving this ? Thanks.

© Stack Overflow or respective owner

Related posts about c++

Related posts about problem