题目如下:
In the United Kingdom the currency is made up of pound (£) and pence (p). There are eight coins in general circulation:
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p), and £2 (200p).It is possible to make £2 in the following way:
1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1pHow many different ways can £2 be made using any number of coins?
这个题目做法比较多:
一般用动态规划的方法,当然小规模的话,也可以用递归
代码如下:
coins = [200,100,50,20,10,5,2,1];
money = 200;
y = change(money,coins);
function y = change(x,c)
if isequal(c,1)
y = 1;
elseif x<0
y = 0;
else
% 分为不用c(1)和用c(1)
y = change(x,c(2:end)) + change(x-c(1),c);
end
end