哈理工校园编程练习赛杭电 acm 2069 A.Coin Change
来源:互联网 发布:c开发数据库应用程序 编辑:程序博客网 时间:2024/05/16 05:48
A.Coin Change
TimeLimit: 1000/1000 MS (Java/Others) Memory Limit:32768/32768 K (Java/Others)
ProblemDescription
Supposethere are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. Wewant 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-centcoin and one 1-cent coin, or two 5-cent coins and one 1-cent coin, or one5-cent coin and six 1-cent coins, or eleven 1-cent coins. So there are fourways of making changes for 11 cents with the above coins. Note that we countthat there is one way of making change for zero cent.
Write a program to find the total number of different ways of making changesfor any amount of money in cents. Your program should be able to handle up to100 coins.
Input
Theinput file contains any number of lines, each one consisting of a number ( ≤250) for the amount of money in cents.
Output
Foreach input line, output a line containing the number of different ways ofmaking changes with the above 5 types of coins.
SampleInput
11
26
SampleOutput
4
13
Author
Lily
今天第一道题 就给整蒙了,虽然能看出来特别简单,但是接触实在有限,算法不懂。
但至少这道题认识到暴力的思路和强大,
此代码很容易理解:从50大钞往前依次检索取遍所有值,但悲剧的是超时;
#include<iostream>using namespace std;int main(){int n,count=0;while(cin>>n){for(int i=0;i<=n;i++)for(int j=0;5*j<=n;j++)for(int a=0;10*a<=n;a++)for(int b=0;25*b<=n;b++)for(int t=0;50*t<=n;t++)if(i+j*5+10*a+25*b+50*t==n&&i+j+a+b+t<=100)count++;cout<<count<<endl;count=0;}return 0;}
一下进行优化,可以这样理解:每次往前进一位,对于剩余的钱数让后面循环看是否能安排好
<pre class="cpp" name="code">#include<iostream>using namespace std;int main(){int n,count=0;while(cin>>n){for(int i=0;i<=n;i++)for(int j=0;5*j<=n-i;j++)for(int a=0;10*a<=n-5*j-i;a++)for(int b=0;25*b<=n-5*j-10*a-i;b++)for(int t=0;50*t<=n-5*j-10*a-i-25*b;t++)if(i+j*5+10*a+25*b+50*t==n&&i+j+a+b+t<=100)count++;cout<<count<<endl;count=0;}return 0;}
<span style="font-size:18px;">这个足以ac</span>
<span style="font-size:18px;">学校提供的代码与之类似:</span>
<pre class="cpp" name="code">#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <cmath>#include <queue>#include <stack>#include <vector>#include <map>#include <set>#include <algorithm>#define max(a,b) ((a)>(b)?(a):(b))#define min(a,b) ((a)<(b)?(a):(b))#define mem(a) memset(a, 0, sizeof(a))#define eps 1e-5#define INF 0x1f1f1f1f#define M 100005using namespace std;int Case = 1;int t, n, m;int main(){ while(~scanf("%d", &n)) { int ans = 0; for(int i = 0; i*50 <= n; i++) { int ii = n-i*50; for(int j = 0; j*25 <= ii; j++) { int jj = ii-j*25; for(int k = 0; k*10 <= jj; k++) { int kk = jj-k*10; for(int l = 0; l*5 <= kk; l++) { int ll = kk-l*5; if(ll >= 0 && i+j+k+l+ll <= 100) { ans++; } } } } } printf("%d\n", ans); } return 0;}
- 哈理工校园编程练习赛杭电 acm 2069 A.Coin Change
- 哈理工校园编程练习赛杭电 acm 4451 C . Dressing
- 哈理工校园编程练习赛杭电 acm 4438 D.Hunters
- 哈理工校园编程练习赛杭电 acm 4442 E.Physical Examination
- 哈理工校园编程练习赛杭电 acm 4256 H.The Famous Clock
- 哈理工校园编程练习赛杭电 acm 4432 G.Sum of divisors
- 哈理工校园编程练习赛杭电 acm F. 4452 Running Rabbits
- 哈理工校园编程练习赛杭电 acm 4450 B.Draw Something
- 哈理工校园编程练习赛杭电 acm 4442 E.Physical Examination
- HDU's ACM 2069 Coin Change
- HDU ACM 11 2069 Coin Change
- HDU 2069 Coin Change
- hdu 2069 Coin Change
- HDU 2069 Coin Change
- HDU 2069 Coin Change
- hdu 2069 Coin Change
- HDOJ 2069 Coin Change
- HDUJ 2069 Coin Change
- 滑动结束 Activity(二)阴影效果和动画的实现
- timer定时器简单例子
- Unicode入门
- 黑马程序员-OC学习笔记-----内存管理
- 华为boss力荐公司高层看的一篇文章,真的很经典!!![转载]
- 哈理工校园编程练习赛杭电 acm 2069 A.Coin Change
- 第二周 阅读程序一
- Android 从零开始
- 九度OJ 题目1080:进制转换
- servlet3.0 用mava打war包报错(webxml attribute is required )
- 5年内买车买房-理财
- [Leetcode] 34. Search for a Range
- 【转】利用百度BAE3.0搭建原版WORDPRESS博客详细教程
- quick-cocos2d-x 学习系列之十三 触摸