USACO 2.3 Money Systems (DP 动态规划 + 空间优化)

本文介绍了一种计算构成特定金额的不同硬币组合数量的算法。通过定义一个数组记录每种金额的组合方式,并逐个考虑每种类型的硬币如何增加组合数量,最终输出构成指定金额的所有可能组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#define DEBUG 1
#define TESTCASES 9

//ways[constructedMoney]表示凑成constructedMoney的数量的钱时的方案数
long long ways[10001];

int main(){
#if DEBUG
	int testCase;
	for (testCase = 1; testCase <= TESTCASES; testCase++){
		char inputFileName[20] = "inputX.txt";
		inputFileName[5] = '1' +  (testCase - 1);
		freopen(inputFileName, "r", stdin);
		printf("\n#%d\n", testCase);
#endif

	 int typesOfCoins, moneyToConstruct;
	 scanf("%d%d", &typesOfCoins, &moneyToConstruct);
		
	 int constructedMoney;
	 for (constructedMoney = 1; constructedMoney <= moneyToConstruct; constructedMoney++)
		 ways[constructedMoney] = 0;
	 ways[0] = 1;

	 int type;
	 for (type = 1; type <= typesOfCoins; type++){
		 int coinsValue;
		 scanf("%d", &coinsValue);
		 for (constructedMoney = coinsValue; constructedMoney <= moneyToConstruct; constructedMoney++)
			 ways[constructedMoney] += ways[constructedMoney - coinsValue];
	 }

	printf("%lld\n", ways[moneyToConstruct]);

#if DEBUG
	}
#endif
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值