开发者

all possible combinations

开发者 https://www.devze.com 2023-01-06 23:45 出处:网络
I have a 开发者_JAVA技巧math problem that goes as follows: I have a container which holds a total of 21000 kilos.

I have a 开发者_JAVA技巧math problem that goes as follows:

I have a container which holds a total of 21000 kilos. I have 4 items A,B,C,D .

Item A weights 1 kilo. Item B weights 4 kilos. Item C weight 5 kilos. Item D weights 5 kilos also.

I am looking for an algorithm that will iterate through all possible combinations keeping the above equation. for example:

{20000 , 0, 0, 200} --> 20000*1 + 0*4 + 0*5 + 200*5 = 21000 kilos.

{19996, 1, 0, 200} --> 19996*1 + 1*4 + 0*5 + 200*5 = 21000 kilos.


This is very similar to the "Counting Change" example from SICP. See:

http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2.2

Example: Counting Change


You have to solve a + 4b + 5c + 5d = 20000 (a,b,c,d >=0)

or a + 4b = 2000 - 5e = 5(400-e) where e = c + d

so a + 4b can be 0, 5, 10, 15, 20, ..., 2000

you can easily find all possible values of a and b from above

after that you know the value of e = c + d, from there you can easily find all possible values of c and d.

0

精彩评论

暂无评论...
验证码 换一张
取 消