ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1244. Gentlemen

Why i got WA ?? pls help me.
Posted by milkkung55 23 Mar 2005 15:52
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.
Posted by (Mek@^__^@)Nakara Kittisirikul 23 Mar 2005 16:22
test with
5
4
2
3
4
5
สิ งำงำ
Still got WA
Posted by milkkung55 23 Mar 2005 16:31
#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);
}