使用递归生成可以在预算上支付的项目列表

时间:2014-10-30 21:28:46

标签: matlab if-statement recursion

递归仍然令我感到困惑。我理解它的基础以及它应该如何工作,但我正在努力实现如何实现它。对于我的功能,我给出了一个具有服装项目和价格的单元格数组,以及预算(给定为双倍)。我必须输出我可以购买的物品的单元格数组(从最便宜到最贵的顺序)并输出我预算中剩余的剩余金额。在购买我需要的所有物品之前,我有可能会没钱,而且我有机会购买我需要的所有东西。这将是我的两个终止条件。我必须使用递归,我不允许在这个问题中使用sort。所以我有点挣扎。主要是弄清楚基本情况。我不明白那一点。或者如何使用两个输入和输出进行递归。所以基本上我的功能看起来像:

function[bought, money] = costumeParty(items, budget)

以下是我要输出的内容:

Test case:

 Costume Items:
'Eyepatch'  8.94000000000000
'Adult-sized Teletubby Onesie'  2.89000000000000
'Cowboy Boots'  1.30000000000000
'Mermaid Tail'  1.75000000000000
'Life Vest' 8.10000000000000
'White Bedsheet With Eyeholes'  4.30000000000000
'Lizard Leggings'   0.650000000000000
'Gandalf Beard' 4.23000000000000
'Parachute Pants'   7.49000000000000
'Ballerina Tutu'    8.75000000000000
'Feather Boa'   1.69000000000000
'Groucho Glasses'   6.74000000000000
'80''s Leg Warmers' 5.08000000000000
'Cat Ear Headband'  6.36000000000000
'Ghostface Mask'    1.83000000000000
'Indoor Sunglasses' 2.25000000000000
'Vampire Fangs' 0.620000000000000
'Batman Utility Belt'   7.08000000000000
'Fairy Wand'    5.48000000000000
'Katana'    6.81000000000000
'Blue Body Paint'   5.70000000000000
'Superman Cape' 4.78000000000000
'Assorted Glow Sticks'  4.07000000000000
'Ash Ketchum''s Baseball Cap'   3.57000000000000
'Hipster Mustache'  6.47000000000000
'Camouflage Jacket' 8.73000000000000
'Two Chains Value Pack' 4.76000000000000
'Toy Pistol'    8.41000000000000
'Sushi Chef Headband'   2.59000000000000
'Pitchfork' 8.57000000000000
'Witch Hat' 4.27000000000000
'Dora''s Backpack'  4.13000000000000
'Fingerless Gloves' 0.270000000000000
'George Washington Wig' 7.35000000000000
'Clip-on Parrot'    4.32000000000000
'Christmas Stockings'   8.69000000000000

很多项目对不起。

[costume1, leftover1] = costumeParty(costumeItems, 5);
     costume1 => {'Fingerless Gloves'
                  'Vampire Fangs'
                  'Lizard Leggings'
                  'Cowboy Boots'
                  'Feather Boa'      }
     leftover1 => 0.47

我有什么:

function[bought, money] = costumeParty(items, budget)
%// I commented these out, because I was unsure of using them:
%// item = [items(:,1)];
%// costumes = [item{:,:}];
%// price = [items{:,2}];

   if budget == 0 %// One of the terminating conditions. I think.
    money = budget;
    bought ={};
%// Here is where I run into issues. I am trying to use recursion to find out the money leftover
else 
    money = costumeParty(items{:,2}) - costumeParty(budget); 
%// My logic here was, costumeParty takes the second column of items and subtracts it from the budget, but it claims I have too many inputs. Any suggestions?
    bought = {items(1,:)};
end

end

如果我能得到一个如何用两个输入/输出进行递归的例子,那就太好了,但我似乎找不到任何东西。谷歌搜索没有帮助。我只是......困惑。

我确实尝试过这样的事情:

function[bought, money] = costumeParty(items, budget)
item = [items(:,1)];
costumes = [item{:,:}];
price = [items{:,2}];

if budget == 0
    money = 0;
    bought ={};

else 
    money = price - budget;
    bought = {items(1,:)};
end

 end

不幸的是,这并不完全是递归的。或者,我认为不是这样,而且无论如何都没有用。做递归的一个技巧是假装函数已经做了你想做的事情(没有你实际编写它),但是它如何与两个输入和输出一起工作?

另一次尝试,因为我将以某种方式想出这个蠢事:

function[bought, money] = costumeParty(items, budget)

price = [items{:,2}]; %// Gives me the prices in a 1x36 double

if price <= budget %// If the price is less than the budget (which my function should calculate) you populate the list with these items
    bought = [costumeParty(items,budget)];
else %// if not, keep going until you run out of budget money. Or something
    bought = [costumeParty(items{:,2},budget)];
end

我想我需要弄清楚如何先对价格进行排序。不使用sort函数。我可能只需要整个递归课程。这个东西让我困惑。我认为不应该这么难.-。

我想我越来越近了!

  function[bought, money] = costumeParty(items, budget)

%My terminating conditions are when I run out of the budget and when buying
%the next item, would break my budget
price = [items{:,2}];
Costumes = [items(:,1)];
[~,c] = size(price);
bought = {};
Locate = [];
List = [];
for j = 1:c %// Need to figure out what to do with this
    [Value, IND] = min(price(:));
    List = [List price(IND)]; 
end

while budget >= 0
        if Value < budget
        bought = {Costumes(IND)};
        money = budget - price(IND);
    elseif length(Costumes) == length(items)
        bought = {Costumes(IND)};
        money = budget - price(IND);
    else
        bought=43; %// Arbitrary, ignore
        budget = budget - price;
    end
    budget = budget - price;
end
duck = 32; %// Arbitrary, ignore

1 个答案:

答案 0 :(得分:1)

根据我对问题的理解,需要使用递归来对项目数组进行排序,然后在排序数组之后,您可以根据预算确定可以购买多少个对象和哪些对象

因此,您需要实现经典的递归排序算法。您可以在网上找到一些,但想法是将整个列表拆分为子列表,并对它们进行相同的排序等等。

实施后,您需要有一个预算门槛。

另一种方法是从2个项目开始。然后,您需要每次在查找最便宜的项目时扫描整个列表,从列表中将其交叉并将下一个函数传递给缺少此项目的项目列表,并且预算将低于该项目。虽然我没有看到这个实现需要递归,但是因为循环在这里已经足够了。

编辑:代码:

这是一个代码的概念,没有运行它,它应该有索引问题(你需要以不同方式解决预算和标签)但我认为它显示了重点。

function main(items,budget)
     boughtItemIndex=itemslist(items,budget)
     moneyLeft=budget;
     for i=1:1:length(boughtItemIndex)
       disp(item(boughtItemIndex(i)))
       moneyLeft=moneyLeft-boughtItemIndex(i);
     end
     disp('Money left:');
     moneyLeft;   

boughtItemIndex=function itemslist(items,budget)
 [minVal minInd]=findmin(items)
 if (budget>minVal)
       newitems=items;
       newitem(minInd)=[];
       newbudget=budget-minVal;
       boughtItemIndex=[minIn, itemlist(newitem,newbudget)];
  end

 [minVal minInd]=function findmin(items)
  minVal=0;
  minInd=0;
  for i=1:1:length(items)
   if (items(i)<minVal)
       minVal=items(i);
       minInd=i;
    end
   end