这是题目
思路一
把质量作为价值,就转化为了01背包
#include <cstdio>
#include <iostream>
using namespace std;
int f[10001],w[10001],v[10001];
int main()
{
int n,m,i,j;
scanf("%d%d",&m,&n);
for(i=1;i<=n;i++)
scanf("%d",&w[i]);
for(i=1;i<=n;i++)
for(j=m;j>=w[i];j--)
f[j]=max(f[j],f[j-w[i]]+w[i]);
printf("%d",m-f[m]);
return 0;
}
思路二
找方案数不为零的最大质量
#include <cstdio>
using namespace std;
int f[20000],a[20000];
int main()
{
int n,i,m,j;
scanf("%d%d",&m,&n);
f[0]=1;
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
for(j=m;j>=a[i];j--)
f[j]+=f[j-a[i]];
for(i=m;i>=1;i--)
if(f[i]!=0) {printf("%d",m-i);break;}
return 0;
}
DO YOU LIKE MI FOR 爱