其实就是会员购买了 40 元的零食盒,现在我根据他的喜好找出来了一些零食,这些零食的价格组成了一个数组,现在需要从这些零食中挑选一些出来组成一盒零食。
|
1
背包啊……
|
|
2
啊,看到了算法在日常中的使用~
|
|
3
背包啊
|
|
4
而且是无限背包 -。 -
|
|
5
lua实现:
local price = {13,11,10,9,6,5,4,3,2,1} local balance = 40 function randomFood() math.randomseed(os.time()) math.random(100) math.random(100) math.random(100) math.random(100) local result = {} local num = #price while balance > 0 do local rangeLeft = 0 for i = 1, num do if price[i] <= balance then rangeLeft = i break end end if rangeLeft == 0 then break end local randomIndex = math.random(rangeLeft, num) local randomPrice = price[randomIndex] balance = balance - randomPrice table.insert(result, randomPrice) print(randomPrice) end print(balance) end randomFood() 楼主拿去玩 |
|
6
没了缩进好丑
不过脚本语言无所谓了 楼主拿去跑就是了 |
|
7
楼主我是来踢场子的:
我觉得这题解法不是很好,如果是要做礼盒,可以按销量分层组一个礼盒。 而不是用单价来组。 |
|
8
=。= 背包9讲
|
|
9
你觉得40个1怎么?
|
|
10
@andychen20121130 如果你有这个需要的话,可以在订单备注中说明
|
|
11
无限背包无误。。
|
|
13
|