hdu1284钱币兑换问题

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

Input
每行只有一个正整数N,N小于32768。

Output
对应每个输入,输出兑换方法数。

Sample Input
2934
12553

Sample Output
718831
13137761

我的代码:

#include<iostream>
using namespace std;
int main()
 {
 int n,j;
 long long sum;
 while(cin>>n)
{ sum=0;
 for(j=0;j<=n/3;j++)
 sum+=(n-j*3)/2+1;
cout<<sum<<endl;
 }
return 0;
}

题解:首先算出最多能换多少个3分的硬币,也就是n/3,再计算当三分硬币为1,2,3到n/3个,含有的二分硬币最多有(n-j3)/2个,因为剩下的都是一分硬币…所以就是有(n-j3)/2种…用sum将他们加起来便是最终答案…

猜你喜欢

转载自blog.csdn.net/weixin_43866317/article/details/85008165