编译c代码时出错
使用分支定界方法的0/1背包的以下代码显示了错误:编译c代码时出错,c,algorithm,knapsack-problem,branch-and-bound,C,Algorithm,Knapsack Problem,Branch And Bound,使用分支定界方法的0/1背包的以下代码显示了错误: 第15行:“项”无法启动参数声明。 第15行:)预期。 函数界也是如此 #include <stdio.h> #include<conio.h> typedef enum {false, true}bool; struct Item { float weight; int value; }; struct Node {
第15行:“项”无法启动参数声明。 第15行:)预期。 函数界也是如此
#include <stdio.h>
#include<conio.h>
typedef enum {false, true}bool;
struct Item
{
float weight;
int value;
};
struct Node
{
int level, profit, bound;
float weight;
};
bool cmp(Item a, Item b)
{
double r1 = (double)a.value / a.weight;
double r2 = (double)b.value / b.weight;
return r1 > r2;
}
int bound(Node u, int n, int W, Item arr[])
{
if (u.weight >= W)
return 0;
int profit_bound = u.profit;
int j = u.level + 1;
int totweight = u.weight;
while ((j < n) && (totweight + arr[j].weight <= W))
{
totweight += arr[j].weight;
profit_bound += arr[j].value;
j++;
}
if (j < n)
profit_bound += (W - totweight) * arr[j].value /
arr[j].weight;
return profit_bound;
}
int knapsack(int W, Item arr[], int n)
{
sort(arr, arr + n, cmp);
queue<Node> Q;
Node u, v;
u.level = -1;
u.profit = u.weight = 0;
Q.push(u);
int maxProfit = 0;
while (!Q.empty())
{
u = Q.front();
Q.pop();
if (u.level == -1)
v.level = 0;
if (u.level == n-1)
continue;
v.level = u.level + 1;
v.weight = u.weight + arr[v.level].weight;
v.profit = u.profit + arr[v.level].value;
if (v.weight <= W && v.profit > maxProfit)
maxProfit = v.profit;
v.bound = bound(v, n, W, arr);
if (v.bound > maxProfit)
Q.push(v);
v.weight = u.weight;
v.profit = u.profit;
v.bound = bound(v, n, W, arr);
if (v.bound > maxProfit)
Q.push(v);
}
return maxProfit;
}
void main()
{
int W = 10; // Weight of knapsack
Item arr[] = {{2, 40}, {3.14, 50}, {1.98, 100},
{5, 95}, {3, 30}};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Maximum possible profit =%d " ,knapsack(W, arr, n));
getch();
}
#包括
#包括
typedef枚举{false,true}bool;
结构项
{
浮重;
int值;
};
结构体类型
{
国际水平,利润,约束;
浮重;
};
bool cmp(项目a、项目b)
{
双r1=(双)a.值/a.重量;
双r2=(双)b.值/b.重量;
返回r1>r2;
}
整数绑定(节点u、整数n、整数W、项目arr[])
{
如果(u.重量>=W)
返回0;
int profit_bound=美国利润;
int j=u级+1;
int总重量=单位重量;
而((jmaxProfit)
Q.push(v);
}
回报最大利润;
}
void main()
{
int W=10;//背包重量
项目arr[]={2,40},{3.14,50},{1.98,100},
{5, 95}, {3, 30}};
int n=sizeof(arr)/sizeof(arr[0]);
printf(“最大可能利润=%d”,背包(W,arr,n));
getch();
}
这是因为项
或节点
的任何使用都需要结构
:
bool cmp(struct Item a, struct Item b)
“C++”有一个内置的代码<代码> BoO[/COD]。它是一个C语言编写的代码。你的标题和代码表明你在请求BoL,但是你把它标记为C++。这不是直的C代码。<代码>队列Q;< /COD>,<代码>!qEMPTY()/<代码>,<代码> q.FrnTo(),…都是C++结构。@ GibbAts最不确定。现代C++与C语言中的相同方法不同。