[Project Euler] Problem 31
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 + 150p + 220p + 15p + 12p + 31p
How many different ways can £2 be made using any number of coins?
典型的换硬币问题,典型的动态规划
#include <iostream>
usingnamespace std;
int main(){
intbase[] ={1,2,5,10,20,50,100,200};
int a[200][8] = {0};
a[0][0]=1;
for(int i=1; i<200; i++){
for(int j=0; j<8; j++){
if(i+1-base[j] >0){
for(int k=0; k<=j; k++){
a[i][j] += a[i-base[j]][k];
}
}
if(i+1-base[j] ==0){
a[i][j] +=1;
}
}
}
int sum =0;
for(int i=0; i<8; i++){
sum += a[199][i];
}
cout << sum << endl;
}
usingnamespace std;
int main(){
intbase[] ={1,2,5,10,20,50,100,200};
int a[200][8] = {0};
a[0][0]=1;
for(int i=1; i<200; i++){
for(int j=0; j<8; j++){
if(i+1-base[j] >0){
for(int k=0; k<=j; k++){
a[i][j] += a[i-base[j]][k];
}
}
if(i+1-base[j] ==0){
a[i][j] +=1;
}
}
}
int sum =0;
for(int i=0; i<8; i++){
sum += a[199][i];
}
cout << sum << endl;
}
把每个面额的换法按最大的零钱面值分成8种情况