For example, item a value 1, item B value 2, item c value 8, and item d value 10 I provide a number 30 to find a combination that requires the minimum number of items used, which can be repeated for example:

Item a value 1,

Item B value 2,

Item c value 8,

D. Item value: 10

I give a number 30

Possible Combinations

The minimum number of items used must be repeated.

Reply content:

For example

Item a value 1,

Item B value 2,

Item c value 8,

D. Item value: 10

I give a number 30

Possible Combinations

The minimum number of items used must be repeated.

@ Ukyoi_D the analysis is correct, but the BFS solution pruning method proposed later is not a common method for dynamic planning. Of course, the method is also correct.

First, standardize the original question:

There are n items. The value of each item is v [I]. and v [0] <= v [I] <= v [I + 1] <= v [n-1]; given the total value w, evaluate the minimum value of m items, make the total value of m Products w.

The algorithm is dynamic planning, and the process has been proved by Ukyoi_D. The state transition equation is provided here.

The state transition equation is:

` f[i][w] = min{ f[i-1][w] if f[i-1][w] exist , f[i-1][w-v[i]] +1 if f[i-1][w-v[i]]exist }`

Where`f[i][w]`

Indicates filling in the first item`w`

Number of items required for valuable items,`f[i][w]`

Before`i`

Items can be combined`w`

Value items, which do not exist, cannot be combined`w`

Valuable items.

For`f[i][w]`

There are actually three possibilities: 1. Select`i`

The number of items must be equal`f[i-1][w-v[i]]+1`

. 2. If I is not selected, the item must be equal`f[i-1][w]`

, 3.`f[i-1][w-v[i]]`

And`f[i-1][w]`

If none of them exist`f[i][w]`

It does not exist either.

Select the smallest or non-existent among the three conditions.

The specific implementation only requires a two-dimensional array, and the nonexistent state is represented by infinity.

This should not involve algorithms, but requires the minimum number of items used, that is, the use of valuable items as much as possible,

Use 30 to take an integer from d-"a, and take the remainder. The integer is the number of the number to use. The remainder is the remaining number. continue to the next operation until the allocation is complete.

// ======================================

First look at the problem:

1. The allocation must be completed without saying 30.

2. requires the minimum number of items

So I think it's okay to give a solution, so I barely count as greedy.

I don't think it's a backpack problem.

1. the knapsack problem is considered in two dimensions: weight and value, but here there is only value

2. The problem with a backpack requires that the final value be maximized. Here, only the minimum number of items is required.

Problem description:

`There are m items, and the value is V1-Vm f (I) for the "value I and the minimum combination of items" f (n)`

Initialization

`f(0) = 0f(i) = +∞，1 <= i <= n`

Solution:

`From 1 to nf (I) = min (f (I-Vx) {1 <= x <= m & I> = Vx }) + 1 if it does not meet 1 <= x <= m & I> = Vx, f (I) = + ∞`

The minimum number of items used is repeated.

That's three d, just 30.

Is there anything less than this?

In addition, this is not a problem with a backpack at all. It is not only a matter of value, but also a matter of weight.

Upstairs, it is obvious that this question does not involve algorithms ...... This is a classic dynamic planning entry ......

The greedy solution is feasible for the specific value of 30, but for example, 16 is not feasible. The optimal solution of 16 is obviously to take two 8 s, instead of the greedy method to take 10 and then take three 2.

In the original question, we should use the minimum number of items to get up to 30. If I take one item at will, for example, I take a 2 item, and then I need to take 28 to meet the requirement of "make up 30, so there was a sub-problem: how to use the minimum number of items to make up 28. Similarly, if I get 10, the sub-problem is that I need 20.

The key to this question is the so-called "optimal sub-structure": When and only when the sub-problem is the optimal solution, the parent problem is the optimal solution. For example, I can have four choices: 1, 2, 8, and 10, so this problem becomes four sub-problems, if I already know that one of the four sub-questions has the least answer, for example, if I need to take k of the four sub-questions, then add the first one I just took, then the original problem requires at least k + 1. Of course, we still don't know which solution is optimal for these four sub-problems, so we use recursive methods to solve them separately, find all secondary subproblems. In all the second-level subproblems, if there is an optimal solution, for example, m is required, then the solution of the first-level subproblem is at least m + 1, the solution to the parent problem is to use m + 2. Optimal sub-structure is the basis for dynamic planning.

For more information about the algorithm implementation, see bluedream's answer. That is the standard method for dynamic planning. The answer below uses recursion. Computers need more resources to process function calls, which is usually slower.

Breadth-first search solution:

The reasoning just now is based on "if you know the solution of the subproblem". Of course, you still don't know the solution of the secondary subproblem, so you can continue to look for it. It is as layered as peeling onions (in the dark it is called "breadth-first search "). (Here is a small trick: Take 8 first, take 2 first, take 2 first, and then take 8 again. After optimization, you can save half a half of the resource)

After several layers are obtained, for example, the latest sub-Problem of a route must be filled with 8. In this case, it is obvious that taking 8 is the optimal solution of this Sub-problem. You can skip other routes because at least one route can meet the requirements. Go back to the optimal solution layer and find the optimal solution (one) for the entire problem ). Of course, there may also be no solution at the end of a route (this question is not possible, because we can get 1, we can always use 1 to make up any positive integer, but if the original question requires 30 0 5 hairs, then obviously there is no solution ), then, discard the route and continue to find other routes until the solution is found. Or traverse all possible situations to find no solution.

Of course, the above is the "Breadth First" method, you can also use the "depth first" method, that is, like walking through the maze, first, follow one of the sub-problems (and the sub-problems of this Sub-problem) to the dark. If it is a dead end, return again and change the road, at this time, the first solution may not be the optimal solution, but with the reference of this solution, all the Hutongs that are deeper than this solution can be marked as dead ends without further drilling, because the optimal solution step is definitely less than the one just found. If we find a better solution later, we will use this better solution as a new reference ...... After all the paths are drilled in this way, either an optimal solution is not found, or the latest solution is the optimal solution.

First, the answers to all hand-calculated results are obviously incorrect.

This is just an example for the subject. In actual situations, the item value and the value to be solved are all program input.

It is impossible to work out a result by hand to say that this question is meaningless.

Secondly, the greedy solution mentioned above @ ekea0407 is incorrect.

Assume that the item weight is 1, 2, 8, and 10 by question, and the weight to be solved is 16. In this case, follow the greedy algorithm:

16 = 10 + 6 = 10 + 2 + 4 = 10 + 2 + 2 + 2, 4 items.

In fact, 16 = 8 + 8, only two items are required.

Finally, the problem is a little different from the problem of a backpack, but it is solved through dynamic planning, and the solution process is similar.

@ Brayden.

The solution to traverse the solution space using brute-force search is poor, because the brute-force search does not store the local solution that has been found, and it will solve the same problem multiple times.

This question is suitable for dynamic planning.

I feel that this issue involves almost no algorithm. use 3 10 first, and start with 30. if there is no need for 10, there will be more than three, and the problem will not be necessary.

Backpack Problems

Dynamic Planning

This is a backpack problem. The above one said that this does not involve algorithms, but it is actually not scientific. The subject can search for the floor instructor's 'backpack 9 thing' on the Internet to solve the problem.