2011年计算机等级考试二级C语言辅导实例编程(6)

2014-10-29 19:30:08 · 作者: · 浏览: 82

  动态规划求0/1背包问题


  #include


  #include


  #include


  //goods是一个或多个物品的重量和价值


  typedef struct goods


  {


  int weight;


  int value;


  } goods;


  //用来定义一个queryList数组


  //数组中的每个元素定义一趟需要记录的数据


  typedef struct queryList


  {


  goods *subResult; //一趟需要记录的数据


  int end; //指示该趟数据的最后一个元素


  } queryList;


  queryList* dknap(goods *Goods, int count, int capacity)


  {


  int i, j, next, pre, index, k;


  queryList *ql;


  goods cur;


  ql = (queryList *)malloc(sizeof(queryList)*count);


  ql[0].subResult = (goods*)malloc(sizeof(goods));


  ql[0].end = 0;


  编辑特别推荐: