第十九周:( Sicily2014) Dairy Queen(c++)

原题链接:http://soj.sysu.edu.cn/2014

Constraints
Time Limit: 1 secs, Memory Limit: 256 MB

Description
Bessie, always in need of an income, has decided to leverage her dairy skills by taking a part-time job at the local Dairy Queen restaurant. She is running a special cash register (since she has hooves instead of fingers and thus requires special accommodation). Her job is making change for customers as they make a purchase.
As she was counting out 83 cents in change, she wondered: “How many ways can I count out 83 cents? I can use three quarters and eight pennies, seven dimes and three pennies, 83 pennies… there must be a zillion ways!”
How many different ways can one make change for N (1 ≤ N ≤ 300) cents using coins from a set of C (1 ≤ C ≤ 8) coins of supplied values C_i (1 ≤ C_i ≤ 200)? “Different” means differing counts of coins.
Thus, 8 cents can be made, in American currency, with 1 five-cent piece + 3 one-cent pieces and also with 8 one-cent pieces. Using 3 one-cent pieces + 1 five-cent piece is the same as 1 five-cent piece + 3 one-cent pieces, so one can create eight cents in just two different ways. Note that some coin systems are poor at making change and result in an answer of ‘0’.
Coin values in the input file are listed in descending order from largest to smallest. All coin values will be distinct.

Input
Line 1: Two space-separated integers: N and C
Lines 2..C+1: Line i+1 contains a single integer: C_i

Output
Line 1: A single line with a single integer that is the number of ways to create N cents of change using the supplied coins. The answer is guaranteed to fit into a signed 32-bit int

Sample Input
83 5
50
25
10
5
1

Sample Output
159

思路:
按照惯例,还是先大概说说题目的大意吧!现实生活中,我们经常会碰到类似的问题。举个例子,假设售货员要向顾客找8毛钱,求共有多少种找法。按照中国第四套货币组成,小于八毛钱的纸币有一毛、两毛和五毛三种。那么,找八毛可以有以下几种不同的找法:(1)一张五毛一张两毛一张一毛(2)一张五毛三张一毛(3)一张两毛六张一毛(4)两张两毛四张一毛(5)三张两毛两张一毛(6)四张两毛(7)八张一毛。
题目中对应的输入是:
8 3
5
2
1
输出为:
7
这是一道关于背包问题的题目,但是跟传统的背包问题又略有不同。传统的背包问题一般是求最值的问题,而这题是求不重复的方法的种类的问题。其实主要思路都一样,不同是在状态转移方程。详细见以下代码。

代码:

#include <iostream>
#include <memory.h>
using namespace std;

int main(){
    int N,C;
    cin>>N>>C;
    int dp[305];
    int Values[10];
    memset(dp,0,sizeof(dp));
    memset(Values,0,sizeof(Values));
    for (int i=1;i<=C;i++)
        cin>>Values[i];
    dp[0]=1;
    for(int i=1;i<=C;i++)
        for(int j=1;j<=N;j++){
            if (j>=Values[i])
                dp[j]+=dp[j-Values[i]];
        }
    cout<<dp[N]<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值