5.最小邮票

有若干张邮票,要求从中选取最少的邮票张数凑成一个给定的总值。

如,有 1 分,3 分,3 分,3 分,4 分五张邮票,要求凑成 10 分,则使用 3 张邮票:3 分、3 分、4 分即可。

输入格式
第一行包含整数 M,表示需要凑成的邮票总值。

第二行包含整数 N,表示给定邮票数量。

第三行包含 N 个整数,表示每个邮票的面值,以升序排列。

输出格式
输出能够凑成总值 M 所需的最少邮票张数。

若无解,则输出 0。

数据范围
1≤M≤100,
1≤N≤20,
每张邮票的面值范围 [1,100]。

输入样例:
10
5
1 3 3 3 4
输出样例:
3

解法1:dfs

#include<iostream>
#include<cstring>
using namespace std;
int m,n;
const int N=10010;
int a[25];
bool st[25];
int cnt;
int ans=2e9;
void dfs(int x,int sum)
{
    if(sum==m) 
    {
        ans=min(ans,cnt);
    }
    else if(x>0)
    {
        st[x]=true;
        sum+=a[x];//当前总和
        cnt++;
        //cout<<sum<<" "<<cnt<<endl;
        dfs(x-1,sum);//选
        st[x]=false;
        sum-=a[x];
        cnt--;
        dfs(x-1,sum);//不选
    }
    return;
}

int main()
{
    cin>>m>>n;
    memset(a,0x3f, sizeof a);//初始化为无穷
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    
    dfs(n,0);
    if(ans==2e9) ans=0;
    cout<<ans<<endl;
    
}

解法2:01背包,从m张邮票选出价值为n的最小方案数

f[i][j] = min (f[ i - 1 ] [ j ], f[ i - 1 ] [ j - a[i] ] + 1);

二维

#include<iostream>
#include<cstring>
using namespace std;
int m,n;
const int N=110;
int a[25];
int f[25][N];


int main()
{
    cin>>m>>n;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i = 1;i <= m;i++) f[0][i] = 0x3f3f3f3f; 
    for(int i=1;i<=n;i++)
        for(int j=0;j<=m;j++)
        {
            f[i][j] = f[i-1][j];
            if(j>=a[i]) f[i][j]=min(f[i-1][j], f[i-1][j-a[i]]+1);
        }
    if(f[n][m]==0x3f3f3f3f) cout<<0<<endl;
    else cout<<f[n][m]<<endl;
    
}

一维

#include<iostream>
#include<cstring>
using namespace std;
int m,n;
const int N=110;
int a[25];
int f[N];


int main()
{
    cin>>m>>n;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i = 1;i <= m;i++) f[i] = 0x3f3f3f3f; 
    for(int i=1;i<=n;i++)
        for(int j=m;j>=a[i];j--)
        {
            f[j]=min(f[j], f[j-a[i]]+1);
        }
    if(f[m]==0x3f3f3f3f) cout<<0<<endl;
    else cout<<f[m]<<endl;
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DESCRIPTION: 1.Analyze Problem A : sorted stamps array A={ai} ai: one stamp element in array n: array size, that is number of elements in array r: desired value of stamps F(n,r):expected result, minimum number of stamps for a given value r from n size array. S: selected stamps array S={si} 2.Choose Algorithm a.Greedy algorithm seems to be a good choice, try to solve it in O(n), i try divide array into subarry B={bi}, r should larger than every elemnt in B that is r>bi and suppose bk is the smallest element in B, so that r= bk%r, f(i,r)=(bk/r), F(n,r)=∑f(i,r). The main idea is to choose the last element who larger than desired value each time. However,it can not give us optimal solution in some condition, like A={8,5,4,1}, if r=10, this algoritm will give a solution F(n,r)=3, S={8,1,1},but the optimal solution should be F(n,r)=2, S={5,5}. b.Full search so the straight forwards algorithm is to search for every solution in A for desired value directly.However, it will always take O(n!) to go through every combination. c.Dynamic programming, at last, I decide to choose dynamic programming. analyze optimal structure, suppose in A={ai}, for a specific stamp ak,there will be two cases it is choosen so that f(i,r)=1+f(i,r-ak) , 1<=i<=k, r>=ak it is not valid so that f(i,r)=f(i-1,r) 3.Design Dynamic programming optimal structure: Compute-opt(r)= 1 + Compute-opt(r-ai) value: Compute-opt(r) = ∞ (r < 0) Compute-opt(r) = 0 (r = 0) Compute-opt(r) = 1+{Compute-opt(r-ai)} ( 1=<i<=n, r>ai>0 ) Complexity :O(nr) Memory cost:O(n+r) Compute in a bottom-up style to recursive every desired value and array. store value of Compute-opt in memory for future use, so that we can easily get value from those memory in future recursive call, and avoid compute again, that is if the array is not change, you can easily fetch result any desired value j (j < r, r is the value using for compute ). 2.For User totally, I design a small command line for this machine list below 1.Manual Operation 2.Self Auto Testing 3.Check Results q.Quit Manual Operation: when select this machine will turn to be manual mode, ask person to input stamps and desired value; Self Auto Testing:when select this machine will turn to be auto mode, show the test case already design in code after that machine will quit automatically. Check Results: only be visiable in Manual Operation, people can check desired value for the array input before, the desired value should be no more than first time input. Quit, clean all the memory and quit system.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值