[code=Cpp width=700px]#include<stdio.h>
#include<stdlib.h>
int maxx(int a,int b)
{
return a>b?a:b;
}
int v[1001],p[101],f[101][101];
int main()
{
int i,j,n,m;
scanf("%d%d",&m,&n);
for(i=1;i<=n;i++)
scanf("%d %d",&v,&p);
for(i=1;i<=n;i++)
{
for(j=m;j>=v;j--)
{
f[j]=maxx(f[i-1][j],f[i-1][j-v]+p);
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
printf("%d ",f[j]);
}
printf("\n");
}
printf("\n");printf("\n");
printf("%d",f[n][m]);
system("pause");
return 0;
}
</stdlib.h></stdio.h>[/code]
[code=Cpp width=700px]#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int v[1001],p[101],f[101];
int main()
{
int i,j,t,c;
scanf("%d%d",&t,&c);
for(i=1;i<=c;i++)
{
scanf("%d%d",&p,&f);
}
for(j=1;j<=c;j++)
{
for(i=t;i>=p[j];i--)
{
if(v[i-p[j]]+f[j]>v)
{
v=v[i-p[j]]+f[j];
}
}
}
printf("%d",v[t]);
//system("pause");
return 0;
}
</string.h></stdlib.h></stdio.h>[/code]
|