有一堆商品,价格分别是 1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,4,4,4,4,5,5,5,6,6,6,7,7,8,8,8,9,9
,给你30块钱,尽量买多个商品。
输出商品编号,第一个算 0 号。第2个算 1号,类推。
Dim fj() As String
Dim zj As Long
Dim i As Long, j As Long
fj = Split("1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,4,4,4,4,5,5,5,6,6,6,7,7,8,8,8,9,9", ",")
'导入价格,已排好序的
zj = 0
i = 0
Do
zj = zj + Val(fj(i))
'求和
i = i + 1
Loop Until zj > 30
'直到总价超过30,退出循环
For j = 0 To i - 2
'退两格,一个是多算一格,一个是超出总价要退一格
Print j;
'输出编号
Next j
Print
'换行
Print zj - Val(fj(i - 1))
'总价退一格,输出