StreamKid
October 29th, 2006, 10:55 AM
I read from file a wanted number, Σ. Then i read N( # of numbers ), and N numbers. I have to create a program that has to find nearest value to Σ that can be "made" using( at maximum ) all the numbers( N ). I must use as few as possible numbers.
Example:
Σ=10, N=6
n1=19, n2=1, n3=8, n4=7, n5=1, n6=2
Wanted number is 10. Nearest value that can be made: we can achieve doing 10.
We can make 10 using:
i) n3 + n6 { 8 + 2 }
ii) n3 + n2 + n5 { 8 + 1 + 1 }
iii) n4 + n5 + n6 { 7 + 1 + 2 }
since ii and iii use 3 numbers to make 10 and i uses 2, the right is i
--------------------------------------------------------------------------------------
that's basically the problem. i 've came up with a solution:
i find the closest number to Σ, i name it x. if any number if larger than Σ, i just "ignore".
I do Σ % x. if, say Σ=100 and x = 90, i get 10. then i find the closest number to 10, and so on.
i 've solved the problem in this way, but that's not always the right solution. does anybody have any idea, any clue or any-something to help???
'd appreciate it much..
tia, streamkid :)
Example:
Σ=10, N=6
n1=19, n2=1, n3=8, n4=7, n5=1, n6=2
Wanted number is 10. Nearest value that can be made: we can achieve doing 10.
We can make 10 using:
i) n3 + n6 { 8 + 2 }
ii) n3 + n2 + n5 { 8 + 1 + 1 }
iii) n4 + n5 + n6 { 7 + 1 + 2 }
since ii and iii use 3 numbers to make 10 and i uses 2, the right is i
--------------------------------------------------------------------------------------
that's basically the problem. i 've came up with a solution:
i find the closest number to Σ, i name it x. if any number if larger than Σ, i just "ignore".
I do Σ % x. if, say Σ=100 and x = 90, i get 10. then i find the closest number to 10, and so on.
i 've solved the problem in this way, but that's not always the right solution. does anybody have any idea, any clue or any-something to help???
'd appreciate it much..
tia, streamkid :)