买债券之完全背包

来源:互联网 发布:数控车床编程教学视频 编辑:程序博客网 时间:2024/04/30 00:07

 

#include <stdio.h>
#define MAX(X,Y) (X)>(Y)?(X):(Y)

struct Bond
{
 int cost;
 int interest;
};
int nBond;
Bond bonds[11];
int Base;
int dp[45300];

int GetMaximum()
{
 int i,j;
 for( i = 0; i <= Base; i++)
 {
  dp[i] = 0;
 }

 for( i = 0;i < nBond; i++)
 {
  for( j = bonds[i].cost; j <= Base; j++)
  {
   dp[j] = MAX(dp[j], dp[j-bonds[i].cost] + bonds[i].interest);
  }
 }
 return dp[Base];

}
int main()
{
 int nCase, nYears;

 scanf("%d", &nCase);
 while(nCase--)
 {
  scanf("%d%d", &Base, &nYears);
  scanf("%d", &nBond);
  int sum = Base;
  for(int i = 0; i < nBond; i++)
  {
   scanf("%d%d", &bonds[i].cost, &bonds[i].interest);
   bonds[i].cost /= 1000;
  
  }
  for(int i = 0;i < nYears; i++)
  {
   Base = sum / 1000;
   sum += GetMaximum();
  }
  printf("%d\n", sum);
 }
 return 0;
}

原创粉丝点击