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 ;
}
 

HDUOJ----Coin Change的更多相关文章

  1. [LeetCode] Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  2. HDOJ 2069 Coin Change(母函数)

    Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. HDU 2069 Coin Change

    Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...

  4. UVA 674 Coin Change(dp)

    UVA 674  Coin Change  解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/ ...

  5. JSU省赛队员选拔赛个人赛1(Coin Change、Fibbonacci Number、Max Num、单词数、无限的路、叠筐)

    JSU省赛队员选拔赛个人赛1 一.题目概述: A.Coin Change(暴力求解.动态规划)     B.Fibbonacci Number(递推求解) C.Max Num(排序.比较) D.单词数 ...

  6. C - Coin Change (III)(多重背包 二进制优化)

    C - Coin Change (III) Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  7. Coin Change (IV) (dfs)

    Coin Change (IV) Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu [Subm ...

  8. Coin Change (II)(完全背包)

                                                               Coin Change (II) Time Limit: 1000MS   Mem ...

  9. [LeetCode] Coin Change 2 硬币找零之二

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  10. UVA.674 Coin Change (DP 完全背包)

    UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...

随机推荐

  1. 微信公众平台开发----自定义菜单、消息处理及获取OpenID(C#)

    一.成为开发者 服务器配置 登录微信公众平台官网后,在公众平台后台管理页面 开发者中心页,点击“修改配置”按钮,填写服务器地址(URL).Token和EncodingAESKey,如下图所示: 其中 ...

  2. MFC增强----任务对话框CTaskDialog类

    /** 注意:从Windows Vista系统才开始支持CTaskDialog类,所以在使用时最好调用 CTaskDialog::IsSupported() 方法做判断 同时:CTaskDialog类 ...

  3. Mahout初体验

    Mahout运行版本: mahout-0.5, mahout-0.6, mahout-0.7,是基于hadoop-0.20.2x的. mahout-0.8, mahout-0.9,是基于hadoop- ...

  4. java多线程之间的顺序问题

    java 多线程: 这样写有问题的:这样写可以的: package com.test; import java.util.concurrent.CountDownLatch; import java. ...

  5. AOP中Advice执行两遍的原因

    在我的spring项目中,Aop的Advice执行了两边,就好像拦截了两遍一样. 原因是:切面应该切到接口的实现类上,而不是接口上

  6. Android 关于操作栏 ActionBar 的设计原则【转载+整理】

    原文地址 本文内容 操作栏目的 基本布局 适应旋转和不同的屏幕尺寸 副操作栏的布局 操作栏按钮 上下文操作栏 操作栏清单 设计原则就是为你在编写 Android APP 时,尤其是如何安排操作按钮的位 ...

  7. mssql批量删除数据库里所有的表

    go declare @tbname varchar(250) declare #tb cursor for select name from sysobjects where objectprope ...

  8. SQL Server 中断开连接到指定数据库的所有连接

    常用的情形是在部署测试数据库时需要通过SQL代码自动重新创建数据库,在删除的时候往往会发生错误,错误信息一般会指出目前有用户连接到这个数据库上,因此不能删除. 实现的方式是通过查询指定数据库中活跃的 ...

  9. stingray前端架构总体设计及运行过程

    SPA 单页应用程序,在一个页面内用ajax技术实现所有的功能的web程序,我们称之为单页应用,明显的特点就是第一次加载之后地址栏非参数部分不再发生变化.大家观察会发现 WIP系统就是一个SPA.我们 ...

  10. 【BZOJ2631】tree

    Description 一棵n个点的树.每一个点的初始权值为1. 对于这棵树有q个操作,每一个操作为下面四种操作之中的一个: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 ...