33,028
社区成员
发帖
与我相关
我的任务
分享#define MY_N 5
#define MY_M 50
int p[MY_N]={10,20,15,46,7};
int w[MY_N]={45,32,78,47,23};z=98 x[5]=1, x[1]=1, x[3]=1, x[2]=1, x[4]=1, is not answer...
z=91 x[1]=1, x[3]=1, x[2]=1, x[4]=1, is not answer...
z=88 x[5]=1, x[3]=1, x[2]=1, x[4]=1, is not answer...
z=81 x[3]=1, x[2]=1, x[4]=1, is not answer...
z=83 x[5]=1, x[1]=1, x[2]=1, x[4]=1, is not answer...
z=76 x[1]=1, x[2]=1, x[4]=1, is not answer...
z=73 x[5]=1, x[2]=1, x[4]=1, is not answer...
z=66 x[2]=1, x[4]=1, is not answer...
z=78 x[5]=1, x[1]=1, x[3]=1, x[4]=1, is not answer...
z=71 x[1]=1, x[3]=1, x[4]=1, is not answer...
z=68 x[5]=1, x[3]=1, x[4]=1, is not answer...
z=61 x[3]=1, x[4]=1, is not answer...
z=63 x[5]=1, x[1]=1, x[4]=1, is not answer...
z=56 x[1]=1, x[4]=1, is not answer...
z=53 x[5]=1, x[4]=1, is not answer...
z=46 x[4]=1, is answer!
Press any key to continue#define MY_N 5
#define MY_M 10
int p[MY_N]={10,20,15,46,7};
int w[MY_N]={45,32,78,47,23};z=98 x[5]=1, x[1]=1, x[3]=1, x[2]=1, x[4]=1, is not answer...
z=91 x[1]=1, x[3]=1, x[2]=1, x[4]=1, is not answer...
z=88 x[5]=1, x[3]=1, x[2]=1, x[4]=1, is not answer...
z=81 x[3]=1, x[2]=1, x[4]=1, is not answer...
z=83 x[5]=1, x[1]=1, x[2]=1, x[4]=1, is not answer...
z=76 x[1]=1, x[2]=1, x[4]=1, is not answer...
z=73 x[5]=1, x[2]=1, x[4]=1, is not answer...
z=66 x[2]=1, x[4]=1, is not answer...
z=78 x[5]=1, x[1]=1, x[3]=1, x[4]=1, is not answer...
z=71 x[1]=1, x[3]=1, x[4]=1, is not answer...
z=68 x[5]=1, x[3]=1, x[4]=1, is not answer...
z=61 x[3]=1, x[4]=1, is not answer...
z=63 x[5]=1, x[1]=1, x[4]=1, is not answer...
z=56 x[1]=1, x[4]=1, is not answer...
z=53 x[5]=1, x[4]=1, is not answer...
z=46 x[4]=1, is not answer...
z=52 x[5]=1, x[1]=1, x[3]=1, x[2]=1, is not answer...
z=45 x[1]=1, x[3]=1, x[2]=1, is not answer...
z=42 x[5]=1, x[3]=1, x[2]=1, is not answer...
z=35 x[3]=1, x[2]=1, is not answer...
z=37 x[5]=1, x[1]=1, x[2]=1, is not answer...
z=30 x[1]=1, x[2]=1, is not answer...
z=27 x[5]=1, x[2]=1, is not answer...
z=20 x[2]=1, is not answer...
z=32 x[5]=1, x[1]=1, x[3]=1, is not answer...
z=25 x[1]=1, x[3]=1, is not answer...
z=22 x[5]=1, x[3]=1, is not answer...
z=15 x[3]=1, is not answer...
z=17 x[5]=1, x[1]=1, is not answer...
z=10 x[1]=1, is not answer...
z=7 x[5]=1, is not answer...
z=0 not find answer!
Press any key to continue#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**只用修改这里*********************/
#define MY_N 5
#define MY_M 200
int p[MY_N]={10,20,15,46,7};
int w[MY_N]={45,32,78,47,23};
/*********************只用修改这里**/
struct node{
int i;
int p;
};
int compar(const void *a,const void *b){
struct node *aa,*bb;
aa=(struct node*)a;
bb=(struct node*)b;
if(aa->p>bb->p) return 1;
if(aa->p==bb->p) return 0;
return -1;
}
void fun(int *p,int *w,int n,int m){
struct node *ls;
char *xk;
int i,j;
int z,sum_p,sum_w,a,b,c,d;
ls=(struct node*)malloc(sizeof(struct node)*n);
xk=(char*)malloc(sizeof(char)*n);
sum_p=0;sum_w=0;
for(i=0;i<n;i++){
ls[i].i=i;
ls[i].p=p[i];
sum_p+=p[i];
sum_w+=w[i];
}
a=m-sum_w;
qsort(ls,n,sizeof(struct node),compar);
for(j=0;;j++){
memset(xk,0,sizeof(char)*n);
d=0;
for(i=0;i<n;i++){
if(j&(1<<i)){
xk[i]=1;d++;
}
}
b=0;c=0;
for(i=0;i<n;i++){
b+=w[ls[i].i]*xk[i];
c+=p[ls[i].i]*xk[i];
}
z=sum_p-c;
printf("z=%d\t",z);
for(i=0;i<n;i++){
if(xk[i]==0) printf("x[%d]=1, ",ls[i].i+1);
}
if(d==n){
printf("\tnot find answer!\n");break;
}
if(-b<=a){
printf("\tis answer!\n\n");break;
}else{
printf("\tis not answer...\n\n");
}
}
free(ls);free(xk);
}
int main(){
fun(p,w,MY_N,MY_M);
return 0;
}#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**只用修改这里*********************/
#define MY_N 5
#define MY_M 200
int p[MY_N]={10,20,15,46,7};
int w[MY_N]={45,32,78,47,23};
/*********************只用修改这里**/
struct node{
int i;
int p;
};
int compar(const void *a,const void *b){
struct node *aa,*bb;
aa=(struct node*)a;
bb=(struct node*)b;
if(aa->p>bb->p) return 1;
if(aa->p==bb->p) return 0;
return -1;
}
void fun(int *p,int *w,int n,int m){
struct node *ls;
char *xk;
int i,j;
int z,sum_p,sum_w,a,b,c;
ls=(struct node*)malloc(sizeof(struct node)*n);
xk=(char*)malloc(sizeof(char)*n);
sum_p=0;sum_w=0;
for(i=0;i<n;i++){
ls[i].i=i;
ls[i].p=p[i];
sum_p+=p[i];
sum_w+=w[i];
}
a=m-sum_w;
qsort(ls,n,sizeof(struct node),compar);
for(j=0;j<=n;j++){
memset(xk,0,sizeof(char)*n);
for(i=0;i<n;i++){
if(j&(1<<i)) xk[i]=1;
}
b=0;c=0;
for(i=0;i<n;i++){
b+=w[ls[i].i]*xk[i];
c+=p[ls[i].i]*xk[i];
}
z=sum_p-c;
printf("z=%d\t",z);
for(i=0;i<n;i++){
if(xk[i]==0) printf("x[%d]=1, ",ls[i].i+1);
}
if(-b<=a){
printf("\tis answer!\n\n");
break;
}else{
printf("\tis not answer...\n\n");
}
}
free(ls);free(xk);
}
int main(){
fun(p,w,MY_N,MY_M);
return 0;
}z=98 x[5]=1, x[1]=1, x[3]=1, x[2]=1, x[4]=1, is not answer...
z=91 x[1]=1, x[3]=1, x[2]=1, x[4]=1, is not answer...
z=88 x[5]=1, x[3]=1, x[2]=1, x[4]=1, is answer!
Press any key to continue

Xk1',Xk2',Xk3',...,Xkn'
0 0 0 ... 0
1 0 0 ... 0
1 1 0 ... 0
...Max Z = p1*X1+p2*X2+...+Pn*Xn
= ∑(pi*Xi)
W1*X1+W2*X2+...+Wn*Xn
= ∑(Wi*Xi) <= MMax Z = ∑[pi*(1-Xi')]
Max Z = (∑pi) - (∑(pi*Xi'))
∑[Wi*(1-Xi')] <= M
-∑(Wi*Xi') <= M-∑Wi ①Xk1,Xk2,...,Xkn
0 0 ... 0
1 0 0
...