蛮力法求解背包问题
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
}
}
int tempi = 0;
while(xx<n+1)
{
printf(" ");
xx++;
}
printf(" ");
while(array[cc] != 0)
{
weight = weight + weightarray[array[cc]];
cc++;
}
while(array[aa] != 0)
printf("\n");
}
}
/*自动生成文件*/
void become()
{Leabharlann Baidu
int i;
FILE *fp;
fp = fopen("in.dat","w");
//srand(unsigned(time(NULL)));
for(i=0;i<n;i++)
{
fprintf(fp,"%d %d\n",rand()%9+1,rand()%41+10);
default:return;
}
return 0;
}
{
value = value + valuearray[array[aa]];
aa++;
}
printf("%-20d",weight);
char *str;
str = "overflow";
if(weight > BAGWEIGHT)
{
printf("%-20s",str);
}
else
{
printf("%-20d",value);
}
fclose(fp);
}
/*删除文件*/
void del()
{
remove("in.dat");
}
int main()
{
printf("请输入背包负重(大于0的整数):");
scanf("%d",&BAGWEIGHT);
printf("请输入物品数量(大于0的整数):");
scanf("%d",&n);
#define MAXSIZE 20000
//#define BAGWEIGHT 200
int a[MAXSIZE] = {0};
int array[MAXSIZE] = {0};
int weightarray[MAXSIZE] = {0};/*存放各物品重量*/
int valuearray[MAXSIZE] = {0};/*存放各物品价值*/
{
int i,xx,cc,weight,value,pp,aa;
weight = 0;
value = 0;
cc = 1;
xx = 1;
aa = 1;
for(i = 1;i <= n;i++)
{
if(a[i])
{
printf("%3d",i);
array[xx] = i;
xx ++;
/* array[]={1,2,3,4}*/
become();
read();
while(k != n)/*蛮力法*/
{
k++;
fun(1,k);
}
i=1;
while(lastvalue[i] != 0)/*找到最优解的总重量和总价值*/
{
if(lastvalue[0]<lastvalue[i])
{
j=lastvalue[i];
lastvalue[i]=lastvalue[0];
lastvalue[0]=j;
u=lastweight[i];
lastweight[i]=lastweight[0];
lastweight[0]=u;
}
i++;
}
gettimeofday(&enda,NULL);
time_usea=(enda.tv_sec-starta.tv_sec)*1000+(enda.tv_usec-starta.tv_usec)/1000;//毫秒
/*程序说明:此程序用来解决蛮力法求解背包问题,运行程序输入背包容量和物品数量,屏幕打印运算过程,最后运算时间输出到文件*/
先看下运行图片
#include <stdio.h>/
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <assert.h>
{
if(a[i])
{
s++;
}
}
return s;
}
/*蛮力法*/
void fun(int t,int k)
{
int i;
if(t > n || jianyan() >= k)
{
return;
}
a[t] = 1;
if(jianyan() == k)
{
print();
}
else
{
fun(t+1,k);
float time_usea=0;
float time_useb=0;
struct timeval starta;
struct timeval enda;
struct timeval startb;
struct timeval endb;
int k = 0,i,j,u,loop;
loop=0;
gettimeofday(&starta,NULL);
lastweight[qq]=weight;
lastvalue[qq]=value;
qq++;
}
printf("\n");
for(pp = 1;pp <= xx;pp++)
{
array[pp] = 0;
}
}
/*检验a[]数组,1的个数*/
int jianyan()
{
int i,s = 0;
for(i = 1;i <= n;i++)
}
a[t] = 0;
fun(t+1,k);
}
/*从文件读取数据*/
void read()
{
int nn = 1,ii = 1;
int i = 1;
FILE *fp;
fp = fopen("in.dat","rb");
while(!feof(fp))
{
if(fscanf(fp,"%d%d",&weightarray[nn],&valuearray[nn]) != EOF)
{
nn++;
i++;
}
else
{
break;
}
}
fclose(fp);
printf(" weight ");
printf("value\n");
for(ii = 1;ii < nn;ii++)
{
printf("no%d: %-5d%-5d",ii,weightarray[ii],valuearray[ii]);
printf("the final weight is %d\n",lastweight[0]);
printf("the final value is %d\n",lastvalue[0]);
printf("It took you %f毫秒\n", time_usea);
FILE *fp = NULL;
fp = fopen("OUT.DAT", "a+");
if(NULL == fp)
{
printf("wrong");
return 0;
}
fprintf(fp, "蛮力: %d %d %f\n", BAGWEIGHT,n,time_usea);
//fprintf(fp, "分支: %d %d %f\n", BAGWEIGHT,n,time_useb/100);
int valuearrayb[MAXSIZE] = {0};
float costarrayb[MAXSIZE] = {0};
int finalb[MAXSIZE] = {0};
int finalweightb[MAXSIZE] = {0};
/*蛮力法输出穷举的每一种可能,并求出下界*/
void print()
fclose(fp);
del();/*要运算多次,生成多次文件,所以每次算完删除文件*/
printf("如果需要再算一次,请按1\n");
printf("如果需要退出,请按2\n");
scanf("%d",&loop);
switch(loop)
{
case 1:return main();
case 2:return;
int lastweight[MAXSIZE]={0};
int lastvalue[MAXSIZE]={0};
int qq=0;
/*上面的数组,变量都是蛮力法所用到,下面的都是分支限界法所用到*/
int BAGWEIGHT;/*背包的载重*/
int n;/*物品的数量*/
int weightarrayb[MAXSIZE] = {0};
}
int tempi = 0;
while(xx<n+1)
{
printf(" ");
xx++;
}
printf(" ");
while(array[cc] != 0)
{
weight = weight + weightarray[array[cc]];
cc++;
}
while(array[aa] != 0)
printf("\n");
}
}
/*自动生成文件*/
void become()
{Leabharlann Baidu
int i;
FILE *fp;
fp = fopen("in.dat","w");
//srand(unsigned(time(NULL)));
for(i=0;i<n;i++)
{
fprintf(fp,"%d %d\n",rand()%9+1,rand()%41+10);
default:return;
}
return 0;
}
{
value = value + valuearray[array[aa]];
aa++;
}
printf("%-20d",weight);
char *str;
str = "overflow";
if(weight > BAGWEIGHT)
{
printf("%-20s",str);
}
else
{
printf("%-20d",value);
}
fclose(fp);
}
/*删除文件*/
void del()
{
remove("in.dat");
}
int main()
{
printf("请输入背包负重(大于0的整数):");
scanf("%d",&BAGWEIGHT);
printf("请输入物品数量(大于0的整数):");
scanf("%d",&n);
#define MAXSIZE 20000
//#define BAGWEIGHT 200
int a[MAXSIZE] = {0};
int array[MAXSIZE] = {0};
int weightarray[MAXSIZE] = {0};/*存放各物品重量*/
int valuearray[MAXSIZE] = {0};/*存放各物品价值*/
{
int i,xx,cc,weight,value,pp,aa;
weight = 0;
value = 0;
cc = 1;
xx = 1;
aa = 1;
for(i = 1;i <= n;i++)
{
if(a[i])
{
printf("%3d",i);
array[xx] = i;
xx ++;
/* array[]={1,2,3,4}*/
become();
read();
while(k != n)/*蛮力法*/
{
k++;
fun(1,k);
}
i=1;
while(lastvalue[i] != 0)/*找到最优解的总重量和总价值*/
{
if(lastvalue[0]<lastvalue[i])
{
j=lastvalue[i];
lastvalue[i]=lastvalue[0];
lastvalue[0]=j;
u=lastweight[i];
lastweight[i]=lastweight[0];
lastweight[0]=u;
}
i++;
}
gettimeofday(&enda,NULL);
time_usea=(enda.tv_sec-starta.tv_sec)*1000+(enda.tv_usec-starta.tv_usec)/1000;//毫秒
/*程序说明:此程序用来解决蛮力法求解背包问题,运行程序输入背包容量和物品数量,屏幕打印运算过程,最后运算时间输出到文件*/
先看下运行图片
#include <stdio.h>/
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <assert.h>
{
if(a[i])
{
s++;
}
}
return s;
}
/*蛮力法*/
void fun(int t,int k)
{
int i;
if(t > n || jianyan() >= k)
{
return;
}
a[t] = 1;
if(jianyan() == k)
{
print();
}
else
{
fun(t+1,k);
float time_usea=0;
float time_useb=0;
struct timeval starta;
struct timeval enda;
struct timeval startb;
struct timeval endb;
int k = 0,i,j,u,loop;
loop=0;
gettimeofday(&starta,NULL);
lastweight[qq]=weight;
lastvalue[qq]=value;
qq++;
}
printf("\n");
for(pp = 1;pp <= xx;pp++)
{
array[pp] = 0;
}
}
/*检验a[]数组,1的个数*/
int jianyan()
{
int i,s = 0;
for(i = 1;i <= n;i++)
}
a[t] = 0;
fun(t+1,k);
}
/*从文件读取数据*/
void read()
{
int nn = 1,ii = 1;
int i = 1;
FILE *fp;
fp = fopen("in.dat","rb");
while(!feof(fp))
{
if(fscanf(fp,"%d%d",&weightarray[nn],&valuearray[nn]) != EOF)
{
nn++;
i++;
}
else
{
break;
}
}
fclose(fp);
printf(" weight ");
printf("value\n");
for(ii = 1;ii < nn;ii++)
{
printf("no%d: %-5d%-5d",ii,weightarray[ii],valuearray[ii]);
printf("the final weight is %d\n",lastweight[0]);
printf("the final value is %d\n",lastvalue[0]);
printf("It took you %f毫秒\n", time_usea);
FILE *fp = NULL;
fp = fopen("OUT.DAT", "a+");
if(NULL == fp)
{
printf("wrong");
return 0;
}
fprintf(fp, "蛮力: %d %d %f\n", BAGWEIGHT,n,time_usea);
//fprintf(fp, "分支: %d %d %f\n", BAGWEIGHT,n,time_useb/100);
int valuearrayb[MAXSIZE] = {0};
float costarrayb[MAXSIZE] = {0};
int finalb[MAXSIZE] = {0};
int finalweightb[MAXSIZE] = {0};
/*蛮力法输出穷举的每一种可能,并求出下界*/
void print()
fclose(fp);
del();/*要运算多次,生成多次文件,所以每次算完删除文件*/
printf("如果需要再算一次,请按1\n");
printf("如果需要退出,请按2\n");
scanf("%d",&loop);
switch(loop)
{
case 1:return main();
case 2:return;
int lastweight[MAXSIZE]={0};
int lastvalue[MAXSIZE]={0};
int qq=0;
/*上面的数组,变量都是蛮力法所用到,下面的都是分支限界法所用到*/
int BAGWEIGHT;/*背包的载重*/
int n;/*物品的数量*/
int weightarrayb[MAXSIZE] = {0};