interface GoodsInt {
name: string
price: number
weight: number
}
function solution(goods: GoodsInt[], capacity: number): void {
let cnt = goods.length
if(cnt === 0) return
let ans = 0
let goodsList: string[] = []
for(let i = 0; i < Math.pow(2, cnt); i ++) {
let n = i, j = 0
let price = 0, weight = 0
let list = []
while(n) {
if(n & 1 && weight < capacity) {
price += goods[j].price
weight += goods[j].weight
list.push(goods[j].name)
}
n >>= 1
j ++
}
if(price > ans && weight <= capacity) {
ans = price
goodsList = list
}
}
console.log(goodsList.join(","), ans)
}
solution([
{
name: '吉他',
price: 1500,
weight: 15
},
{
name: '音响',
price: 2500,
weight: 25
},
{
name: '房子',
price: 2000,
weight: 20
},
], 35)