哈理工校园编程练习赛杭电 acm 2069 A.Coin Change

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值