Coin Change

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10590    Accepted Submission(s): 3535

Problem Description
Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money.

For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, or two 5-cent coins and one 1-cent coin, or one 5-cent coin and six 1-cent coins, or eleven 1-cent coins. So there are four ways of making changes for 11 cents with the above coins. Note that we count that there is one way of making change for zero cent.

Write a program to find the total number of different ways of making changes for any amount of money in cents. Your program should be able to handle up to 100 coins.

 
Input
The input file contains any number of lines, each one consisting of a number ( ≤250 ) for the amount of money in cents.
 
Output
For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.
 
Sample Input
11
26
 
Sample Output
4
13
 
Author
Lily
 
Source
 

思路: 此题可以采取dfs,但是用分治法还是可以的,优化一下可以达到15ms......

在此贴出代码:

 #include<iostream>
using namespace std;
int main()
{
int n,count;
int j,k,m,g,l;
while(cin>>n)
{
count=;
for( j=;j<=n/;j++) //
{
k=m=g=l=;
if(n==j*+k*+m*+g*+l)
{
count++;
break;
}
else
if(n<j*+k*+m*+g*+l)
break; for( k=;k<=n/;k++) //
{
m=g=l=;
if(n==j*+k*+m*+g*+l)
{
count++;
break;
}
else
if(n<j*+k*+m*+g*+l)
break;
for( m=;m<=n/;m++) //
{
g=l=;
if(n==j*+k*+m*+g*+l)
{
count++;
break;
}
else
if(n<j*+k*+m*+g*+l)
break;
for( g=;g<=n/;g++) //
{
l=;
if(n==j*+k*+m*+g*+l)
{
count++;
break;
}
else
if(n<j*+k*+m*+g*+l)
break;
for( l=;l<=-j-k-m-g;l++) //
{
if(n==j*+k*+m*+g*+l)
{
count++;
break;
}
else
if(n<j*+k*+m*+g*+l)
break;
}
}
}
}
} cout<<count<<endl;
}
return ;
}
 

最新文章

  1. hdu1754 I Hate It
  2. oracle-1
  3. 添加 SecondaryNameNode
  4. 修改webapp底图
  5. 练习题之Wait/Notify
  6. leptonica 学习笔记1
  7. POCISO-採购创建内部订单(R12.2.3)
  8. Web Service相关工具的配置
  9. offsetParent和parentNode区别
  10. oracle整体知识的大致介绍(1)-概念
  11. cocos2d-x3.2中map的基本操作和使用
  12. 关于CGI、FastCGI和PHP-FPM的关系
  13. c# 操作数据库
  14. 全文检索选择-------- Elasticsearch与Solr
  15. 英语笔记3(git)
  16. PUTTY工具的使用
  17. Struts2学习第四天——全局结果,动态结果及异常映射
  18. 【Oracle 】pctfree和pctused详解
  19. UVM中factory机制的使用
  20. 【大数据之数据仓库】kudu性能测试报告分析

热门文章

  1. python3 验证码图片切割
  2. 4.3 使用 SQL 语句操作数据框
  3. unity3d Player Settings 中的Stripping Level(剥离等级)对应每个等级具体剥离了哪些库
  4. Shader开发工具: PVRShaman
  5. 运行代码时报linker command failed with exit code 1 错误
  6. OAuth2 Demo PHP
  7. 启动IntelliJ IDEA 2016报错:cannot start under Java 1.7 : Java 1.8 or later is required 解决办法
  8. Asp.NET websocket,Asp.NET MVC 使用 SignalR 实现推送功能一(Hubs 在线聊天室)
  9. 从头来之【MAC下代码管理工具】
  10. Linux常用命令之wget