Advertisement
nontawat1996

0019

Dec 23rd, 2011
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. int main()
  5. {
  6.     int n,i,j,s[10]={0},b[10]={0},k,l,t;
  7.     long int totals,totalb,min=1000000001;
  8.     char bit[32]={'0'};
  9.     scanf("%d",&n);
  10.     for(i=0;i<n;i++) scanf("%d %d",&s[i],&b[i]);
  11.     k=pow(2,n);
  12.  
  13.     for(i=1;i<k;i++)
  14.     {
  15.         //printf("\n");
  16.         totals=1;
  17.         totalb=0;
  18.         l=0;
  19.         t=i;
  20.         do
  21.         {
  22.             bit[l++]=(t%2)+'0';
  23.             t/=2;
  24.         }
  25.         while(t>0);
  26.  
  27.         //printf("l = %d ",l-1);
  28.         for(t=l-1;t>=0;t--)
  29.         {
  30.             //printf("%c",bit[t]);
  31.             if(bit[t]=='1')
  32.             {
  33.                 totals*=s[t];
  34.                 totalb+=b[t];
  35.                 bit[t]='0';
  36.             }
  37.         }
  38.         t=abs(totals-totalb);
  39.         if(t==0)
  40.         {
  41.             printf("0");
  42.             return 0;
  43.         }
  44.         if(t<min) min=t;
  45.     }
  46.     printf("%d",min);
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement