哈理工校园编程练习赛杭电 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;}

 

0 0
原创粉丝点击