文章标题

题目大意:
给出一个数字,可以用五种面值的钱币拼成,可以有多少种方法。

代码:

#include <iostream>
using namespace std;
#include <cstring>
#include <stdio.h>

int coin[5] = {1,5,10,25,50};
long long dp[8000];

int main() {

    int n;
// memset(dp,1,sizeof(dp));
    dp[0] = 1;
    for(int i = 0; i <5;i++) {
        for(int j = 0; j < 7900; j++)
            dp[j + coin[i]] += dp[j];
    }
    while(scanf("%d",&n)!=EOF){
        printf("%lld\n",dp[n]);
    }
    return 0;
}

#include <iostream>
using namespace std;
#include <cstring>
#include <stdio.h>
const int MAXN = 8000;
int coin[5] = {1,5,10,25,50};
long long dp[MAXN][5];

long long solve(int i,int sum) {
    if(dp[sum][i] != -1)
        return dp[sum][i];
    dp[sum][i] = 0;
    for(int j = i ; j < 5 && sum >= coin[j]; j++) {
        dp[sum][i] += solve(j,sum - coin[j]);
    }
    return dp[sum][i];
}
int main() {
    int n;
    memset(dp,-1,sizeof(dp));
    for(int i =0 ; i < 5; i++)
        dp[0][i] = 1;
    while(scanf("%d",&n)!=EOF) {
        printf("%lld\n",solve(0,n));
    }
    return 0;
}

你可能感兴趣的:(文章标题)