【发布时间】:2016-06-24 21:11:04
【问题描述】:
我被要求解决这个简单的问题,而我的编程能力相当惨。在这里,
给定以下项目,找出所有服装项目的组合,使总成本正好是 100 美元。
这是我的代码:
tshirt=20; %price of tshirt
shorts=15; %price of shorts
socks=5; %price of socks
solution=0;
for i=20 %cannot have more than 20 socks (over $100)
for j = 6 %cannot have more than 6 shorts (over $100)%cannot have more than 20 socks (over $100)
for k=5 %cannot have more 5 tshirts (over $100)
%Some code or function that will add them up so they are
%exactly $100??
tshirt+shorts+socks==100
end
end
end
我知道这段代码很原始,但我不知道如何处理...... 任何帮助将不胜感激。
【问题讨论】:
-
这基本上是硬币找零的问题,它会是一个好的开始搜索。
-
最终等式应该看起来像 i * tshirt+j * shorts+k * socks==100。我不记得 Matlab 但通常你应该有: if (i * tshirt+j * shorts+k * socks==100) solution=solution+1
标签: algorithm matlab combinations