Home  >  Article  >  Backend Development  >  Find which numbers sum close to a given value (less than or equal to)

Find which numbers sum close to a given value (less than or equal to)

WBOY
WBOYOriginal
2016-07-06 13:53:551523browse

In life, there is often a situation like this:

Online shopping, a fixed shopping coupon of 1,000 yuan, there are many products with different unit prices, 101, 230, 330, 210, 299,...
How can I use up this 1,000 yuan as much as possible.

This is just an example. I don’t know how to express it specifically when it is converted into a computer professional description.
For example: Find which numbers the sum of is close to a given value (less than or equal to)

What algorithm should be used for such a business logic? No language limit. (c,php,java,node)

Reply content:

In life, there is often a situation like this:

Online shopping, a fixed shopping coupon of 1,000 yuan, there are many products with different unit prices, 101, 230, 330, 210, 299,...
How can I use up this 1,000 yuan as much as possible.

This is just an example. I don’t know how to express it specifically when it is converted into a computer professional description.
For example: Find which numbers the sum of is close to a given value (less than or equal to)

What algorithm should be used for such a business logic? No language limit. (c,php,java,node)

This kind of problem belongs to the category of 背包问题 and can be solved using 贪心算法.

01 Backpack Problem

This is a typical 01背包问题, the most common solution is to use 动态规划

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn