博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZOJ 1666 G-Square Coins
阅读量:5239 次
发布时间:2019-06-14

本文共 1787 字,大约阅读时间需要 5 分钟。

 

People in Silverland use square coins. Not only they have square shapes but also their values are square numbers. Coins with values of all square numbers up to 289 (=17^2), i.e., 1-credit coins, 4-credit coins, 9-credit coins, ..., and 289-credit coins, are available in Silverland.

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

The input consists of lines each containing an integer meaning an amount to be paid, followed by a line containing a zero. You may assume that all the amounts are positive and less than 300.

Output

For each of the given amount, one line containing a single integer representing the number of combinations of coins should be output. No other characters should appear in the output.

 

Sample Input

2
10
30
0

Sample Output

1
4
27

 

时间复杂度:

题解:动态规划

代码:

#include 
using namespace std;int coin[20] = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289};int a[333][333];int money;int main() { while(~scanf("%d", &money)) { memset(a, 0, sizeof(a)); if(money == 0) break; a[0][0] = 1; for(int i = 0; i < 17; i ++) { for(int j = coin[i]; j <= money; j ++) { for(int k = 1; k < 300; k ++) { if(j >= coin[i]) a[k][j] += a[k - 1][j - coin[i]]; } } } int sum = 0; for(int i = 0; i < 300; i ++) sum += a[i][money]; printf("%d\n", sum); } return 0;}

  

转载于:https://www.cnblogs.com/zlrrrr/p/9529095.html

你可能感兴趣的文章
新的开始
查看>>
Leetcode 226: Invert Binary Tree
查看>>
解决miner.start() 返回null
查看>>
bzoj 2007: [Noi2010]海拔【最小割+dijskstra】
查看>>
C# Dynamic通用反序列化Json类型并遍历属性比较
查看>>
auth模块
查看>>
javascript keycode大全
查看>>
前台freemark获取后台的值
查看>>
log4j.properties的作用
查看>>
游戏偶感
查看>>
Leetcode: Unique Binary Search Trees II
查看>>
C++ FFLIB 之FFDB: 使用 Mysql&Sqlite 实现CRUD
查看>>
Spring-hibernate整合
查看>>
c++ map
查看>>
exit和return的区别
查看>>
discuz 常用脚本格式化数据
查看>>
洛谷P2777
查看>>
PHPStorm2017设置字体与设置浏览器访问
查看>>
Django 相关
查看>>
git init
查看>>