有1分,2分,5分,10分四种硬币,每种硬币数量无限,给定n分钱,求有多少种组合可以组合成n分钱?

使用回溯法

int countQ = 0;
int Target = 0;

int coin[4] = {1,2,5,10};
int total=0;
vector solution;

void dfs(int index)
{
	if(total == Target)
	{
		countQ++;
		cout< Target)
		return;

	for(int i = index; i < 4; i++)
	{
		total += coin[i];
		solution.push_back(coin[i]);
		dfs(i);
		solution.pop_back();
		total-=coin[i];
	}
}



int main() {
	Target = 50;
	dfs(0);
	cout<


你可能感兴趣的:(算法)