|
|
вернуться в форумWhy i got WA ?? pls help me. this is my sorce #include<stdio.h> #include<stdlib.h> #include<math.h> void main() { int m,n,b,i,k,in,sum,ans=0,index,full=0,left,*weight,*bi,*result; scanf("%d",&m); scanf("%d",&n); bi=(int*)malloc(sizeof(int)*n); result=(int*)malloc(sizeof(int)*n); weight=(int*)malloc(sizeof(int)*n); for(i=0;i<n;i++) { *(bi+i)=*(result+i)=*(weight+i)=0; } for(i=0;i<n;i++) { scanf("%d",weight+i); full+=*(weight+i); } left=full-m; for(b=0;b<pow(2,n);b++) { sum=0; index=0; if(ans>1) break; for(i=n-1;i>=0;i--) { k=b; *(bi+(n-1-i))=(k>>i)&1; } for(i=0;i<n;i++) { if(*(bi+i)==1) { sum+=*(weight+i); if(ans==0) { *(result+index)=i+1; index++; in=index; } } } if(sum==left) ans++; } if(ans==0) printf("0"); else if(ans>1) printf("-1"); else if(ans==1) for(i=0;i<in;i++) printf("%d ",*(result+i)); } Thanks a lot. Re: Why i got WA ?? pls help me. test with 5 4 2 3 4 5 สิ งำงำ Still got WA #include<stdio.h> #include<stdlib.h> #include<math.h> void main() { int n,i,in,ans=0,index,*weight,*bi,*result; unsigned long sum,m,left,full=0,b,k; scanf("%ld",&m); scanf("%d",&n); bi=(int*)malloc(sizeof(int)*n); result=(int*)malloc(sizeof(int)*n); weight=(int*)malloc(sizeof(int)*n); for(i=0;i<n;i++) { *(bi+i)=*(result+i)=*(weight+i)=0; } for(i=0;i<n;i++) { scanf("%d",weight+i); full+=*(weight+i); } left=full-m; for(b=0;b<pow(2,n);b++) { sum=0; index=0; if(ans>1) break; for(i=n-1;i>=0;i--) { k=b; *(bi+(n-1-i))=(k>>i)&1; } for(i=0;i<n;i++) { if(*(bi+i)==1) { sum+=*(weight+i); if(ans==0) { *(result+index)=i+1; index++; in=index; } } } if(sum==left) ans++; } if(ans==0) printf("0"); else if(ans>1) printf("-1"); else if(ans==1) for(i=0;i<in;i++) printf("%d ",*(result+i)); free(bi); free(result); free(weight); } |
|
|