第二期一题

问题:在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法。请你编程序计算出共有多少种兑法。

source:
杭电ACM集训队训练赛(VII)

分析;一份币作为填充物,在无三分币的时候,,通过对二分币数量最大可容纳数量,作为这种情况的兑换方法数。然后在只有一个三分币时…在两个三分币时…以此类推
最后把所有情况相加就是总的方法。

#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
	int  n,b;
	while(cin>>n)
	{
		int t=0;
		if(n<3)
		{
			cout<<n<<endl;
		}
		else
		{
		b=n/3;
		for(int i=0;i<=b;i++)
		{
			t=(n-i*3)/2+t;
		}
		cout<<t+b+1<<endl;
		}
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/weixin_43792166/article/details/84977621