51Nod

51Nod - 1101 换零钱

N元钱换为零钱,有多少不同的换法?币值包括1 2 5分,1 2 5角,1 2 5 10 20 50 100元。
 
例如:5分钱换为零钱,有以下4种换法:
1、5个1分
2、1个2分3个1分
3、2个2分1个1分
4、1个5分
(由于结果可能会很大,输出Mod 10^9 + 7的结果)
Input
输入1个数N,N = 100表示1元钱。(1 <= N <= 100000)
Output
输出Mod 10^9 + 7的结果
Input示例
5
Output示例
4

题解: 

   使用无限背包解决。 

#include <iostream> 
#include <cstdio> 
#include <cstdlib> 
#include <cstring>  
using namespace std;
const int MAXN = 100005; 
const int MOD = 1e9 + 7; 
const int coin[13] = {1,2,5,10,20,50,100, 200, 500, 1000, 2000, 5000, 10000 }; 

int n, dp[MAXN]; 

void init(){
	memset(dp, 0, sizeof(dp)); 
	dp[0] = 1; 
	for(int i =0; i<13; ++i){
		for(int j=coin[i]; j<MAXN; ++j){
			dp[j] = ( dp[j] + dp[j- coin[i]] ) % MOD; 
		}
	}
}

int main(){

	init();  
	while(scanf("%d", &n) != EOF){
		printf("%d
", dp[n] );
	}
	return 0; 
}

  

原文地址:https://www.cnblogs.com/zhang-yd/p/6809910.html