Click here to Skip to main content
15,897,315 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I Have a user input value e.g = 3360 or any value like 4500, 5000, 3900, 2000 etc.

I have 3 standard value = 1120, 2240, 3360 //these are fixed length

My question here is, If user input a value 3360 then how can I show the possible value like:-

1120 2240 3360
3 0 0 // It sum 3360
1 1 0 // It sum 3360
0 0 1 // It sum 3360

What I have tried:

I am never done these type of work, Please help.
Posted
Updated 17-Nov-18 0:57am

1 solution

Start by thinking about the numbers you have to work with.
Let's call them x, y, and z, and call the target the user entered t
So you are looking for a solutions where
a * x + b * y * c * z = t

There are two possible outcomes here:
1) There is no combination of the three numbers that equals t. This will be the most common!
2) There are combinations.
So start with the largest one of x, y , and z.
Subtract it from t. If the result is positive, set t to it, and count it. Go back around and do it again.
If not, there are no more large values that can be fitted in there, so repeat the exercise with the next biggest., and so on until you run out of x, y , and z.
If at any point t = 0, you're done and found a match.

To find the next matches, start with the second largest, and repeat the process.

Try it on paper, and you'll see what I mean.

I'd suggest that this is probably well suited to a recursive solution, but the code is up to you - this is your homework, after all!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900