upc-Optimal Coin Change(完全背包+路径回溯记录)

38 篇文章 0 订阅
35 篇文章 1 订阅
题目描述

In a 10-dollar shop, everything is worthy 10 dollars or less. In order to serve customers more effectively at the cashier, change needs to be provided in the minimum number of coins.
In this problem, you are going to provide a given value of the change in different coins. Write a program to calculate the number of coins needed for each type of coin.
The input includes a value v, a size of the coinage set n, and a face value of each coin, f1, f2, …, fn. The output is a list of numbers, namely, c1, …, cn, indicating the number of coins needed for each type of coin. There may be many ways for the change. The value v is an integer satisfying 0 < v ≤ 2000, representing the change required
in cents. The face value of a coin is less than or equal to 10000. The output of your program should take the combination with the least number of coins needed.
For example, the Hong Kong coinage issued by the Hong Kong Monetary Authority consists of 10 cents, 20 cents, 50 cents, 1 dollar, 2 dollars, 5 dollars and 10 dollars would be represented in the input by n = 7, f1 = 10, f2 = 20, f3 = 50, f4 = 100, f5 = 200, f6 = 500, f7 = 1000.

输入

The test data may contain many test cases, please process it to the end of the file.
Each test case contains integers v, n, f1, …, fn in a line. It is guaranteed that n ≤ 10 and 0 < f1 < f2 < …< fn.

输出

The output be n numbers in a line, separated by space. If there is no possible change, your output should be a single −1. If there are more than one possible solutions, your program should output the one that uses more coins of a lower face value.

样例输入
2000 7 10 20 50 100 200 500 1000
250 4 10 20 125 150
35 4 10 20 125 150
48 4 1 8 16 20
40 4 1 10 13 37
43 5 1 2 21 40 80
样例输出
0 0 0 0 0 0 2
0 0 2 0
-1
0 1 0 2
3 0 0 1
1 1 0 1 0
题意

给你 v 块钱,有 n 种面值的硬币,第 i 个硬币的面额为 f [ i ] (硬币数量不限),问你能否正好凑出 v 块钱,不能的话输出 - 1 ,能的话输出使用硬币数最少的方案。

思路

完全背包( 这里转移方程是: f [ j ] = min ( f [ j ] , f [ j - a [ i ] ] + 1 ) ( j 是 j 块钱, i 是第 i 种硬币 ,这里取最小的,因为尽量要硬币数最少,同时 f [ j ] 数组记得初始化)),还有就是路径的回溯与记录,详情请看代码吧。。。。

代码
#include<iostream>
#include<string>
#include<map>
#include<set>
//#include<unordered_map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<fstream>
#define X first
#define Y second
#define best 131 
#define INF 0x3f3f3f3f
#define P pair<int,int>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps=1e-7;
const double pai=acos(-1.0);
const int N=2e4+10;
const int maxn=4e5+10;
const int mod=1e9+7;
//const int d[4][2]={0,1,1,0,-1,0,0,-1};
int f[2010],a[10010],ans[10010],v,n;
void init()
{
    for(int i=1;i<=2010;i++) f[i]=INF;
    for(int i=1;i<=n;i++) ans[i]=0;
}
int main()
{
//  ios::sync_with_stdio(false);
    //memset(mp,-1,sizeof(mp));
    while(scanf("%d",&v)!=EOF)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        init();f[0]=0;
        for(int i=1;i<=n;i++)
            for(int j=a[i];j<=v;j++) 
                f[j]=min(f[j],f[j-a[i]]+1);
        if(f[v]==INF) printf("-1\n");
        else
        {
            int tmp=v;
            while(tmp)
            {
                for(int i=n;i>=1;i--)
                {
                    if(tmp>=a[i]&&f[tmp]==f[tmp-a[i]]+1)
                    {
                        ans[i]++;
                        tmp=tmp-a[i];
                        break;
                    } 
                }
            }
            for(int i=1;i<n;i++) printf("%d ",ans[i]);
            printf("%d\n",ans[n]);
        }
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值