题意
有5种分值的硬币无数,分别是50、25、10、5、1. 每给一个分值n(n<=250),输出用这5种硬币兑换的方案数,要求每个方案所用硬币数不超过100。规定分值n为0时方案数为1。
Sample Input
11
26
Sample Output
4
13
打表。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 251;
int c1[N][101],c2[N][101]; //c[j][t]表示用t个硬币凑出j分值的方案数
int c3[N]; //c3[i]表示分值i对应的方案数
const int cent[]={1,5,10,25,50};
int main(){
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
memset(c3,0,sizeof(c3));
c1[0][0]=1;
for(int i=1; i<=5; i++){
for(int j=0; j<N; j++){
for(int k=0; k*cent[i-1]+j<N; k++)
for(int t=0; t+k<=100; t++){ //t表示所用硬币数
c2[k*cent[i-1]+j][t+k]+=c1[j][t];
}
}
for(int j=0; j<N; j++)
for(int t=0; t<=100; t++){
c1[j][t]=c2[j][t];
c2[j][t]=0;
}
}
for(int i=0;i<N;i++)
for(int j=0;j<=100;j++)
c3[i]+=c1[i][j];
//pay attention:c3[0]=1
//如果不计算i=0,则c3[0]=0
//c3[0]的值视情况而定
int n;
while (~scanf("%d",&n)){
printf("%d\n",c3[n]);
}
return 0;
}