?? a.c
字號:
# include <stdio.h>
# define N 100
double limitW,totV,maxV;
int option[N],cop[N];
struct { double weight;
double value;
}a[N];
int n;
void find(int i,double tw,double tv)
{ int k;
/*考慮物品i包含在當前方案中的可能性*/
if (tw+a[i].weight<=limitW)
{ cop[i]=1;
if (i<n-1) find(i+1,tw+a[i].weight,tv);
else
{ for (k=0;k<n;k++)
option[k]=cop[k];
maxV=tv;
}
cop[i]=0;
}
/*考慮物品i不包含在當前方案中的可能性*/
if (tv-a[i].value>maxV)
if (i<n-1) find(i+1,tw,tv-a[i].value);
else
{ for (k=0;k<n;k++)
option[k]=cop[k];
maxV=tv-a[i].value;
}
}
void main()
{ int k;
double w,v;
printf("輸入物品種數\n");
scanf("%d",&n);
printf("輸入各物品的重量和價值\n");
for (totV=0.0,k=0;k<n;k++)
{ scanf("%1f%1f",&w,&v);
a[k].weight=w;
a[k].value=v;
totV+=v;
}
printf("輸入限制重量\n");
scanf("%1f",&limitW);
maxV=0.0;
for (k=0;k<n;k++) cop[k]=0;
find(0,0.0,totV);
for (k=0;k<n;k++)
if (option[k]) printf("%4d",k+1);
printf("\n總價值為%.2f\n",maxV);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -