In England the currency is made up of pound, £, and pence, p, and 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×1p
How many different ways can £2 be made using any number of coins?
|
Go to the thread for problem 31 in the forum.
Download overview for problem 31.
见到这个题目我的第一感觉就是要用我的第一篇博文关于整数划分的一篇介绍:http://blog.csdn.net/zhangzhengyi03539/article/details/47102345
另一种思路就是使用类似于斐波那契数列求解的方法
#include <iostream> using namespace std; const int maxn = 200; int coin[8] = { 1, 2, 5, 10, 20, 50, 100, 200 }; int dp[maxn+1]; int m; int main() { int i, j; memset(dp, 0, sizeof(dp)); dp[0] = 1; for (i = 0; i<8; i++) { for (j = coin[i]; j <= maxn; j++) { if (j >= coin[i]) dp[j] += dp[j - coin[i]]; } } printf("%d\n", dp[maxn]); return 0; }