SDUT背包(01)换零钱

https://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2396/pid/2777.html

有背包原理并是不是背包问题。

通过滚动数组来求出和 。

而不是用背包来求最优

 
 

1.01背包:

[cpp]  view plain  copy
  1. //从前一个转态转移过来,选还是不选  
  2.     for (int i=1; i<=N; i++){  
  3.         for (int j=0; j<=M; j++){  
  4.             if (weight[i]<=j){  
  5.                 f[i][j]=max(f[i-1][j],f[i-1][j-weight[i]]+value[i]);  
  6.             }  
  7.             else  
  8.                 f[i][j]=f[i-1][j];  
  9.         }  
  10.     }  
    1. //一维滚动数组  
    2. for (int i=1; i<=N; i++)  
    3.     for (int j=M; j>=1; j--)  
    4.         if (weight[i]<=j)  
    5.             f[j]=max(f[j],f[j-weight[i]]+value[i]);  

3.完全背包

[cpp]  view plain  copy
  1. //每件物品可以使用无数次,求最大值  
  2. memset(dp,-INF,sizeof(dp));  
  3. dp[0]=0;  
  4. for(int i=0;i<m;i++)  
  5.     for(int j=c[i];j<=v;j++)  
  6.         dp[j]=max(dp[j],dp[j-c[i]]+w[i]);  
#include <iostream>

using namespace std;

int main()
{
int a[100005],n,x[3]={1,2,3},i,j;
a[0]=1;
for(i=0;i<3;i++)
{
for(j=x[i];j<=32777;j++)
a[j]=a[j]+a[j-x[i]];
}
while(cin>>n)
{
    cout <<a[n]<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/beposit/article/details/80681265
今日推荐