Square Coins
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 39 Accepted Submission(s) : 27
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
There are four combinations of coins to pay ten credits:
ten 1-credit coins,
one 4-credit coin and six 1-credit coins,
two 4-credit coins and two 1-credit coins, and
one 9-credit coin and one 1-credit coin.
Your mission is to count the number of ways to pay a given amount using coins of Silverland.
Input
Output
Sample Input
2 10 30 0
Sample Output
1 4
27
同以上用数字去凑大数字一样,只不过这道题小数字换成了i*i的数值,按照常规来就可以了。
#include<iostream>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
int main(){
int n;
int a[20];
for(int i=1;i<=19;i++)
a[i]=i*i;
while(cin>>n,n)
{
int wz;
int i,j,k;
int dp[1111];
memset(dp,0,sizeof(dp));
dp[0]=1;
for(i=19;i>=1;i--)
{
if(a[i]<=n)
{
wz=i;
break;
}
}
for(i=1;i<=wz;i++)
{
for(j=a[i];j<=n;j++)
dp[j]+=dp[j-a[i]];
}
cout<<dp[n]<<endl;
}
return 0;
}
325




被折叠的 条评论
为什么被折叠?



