Tuesday, 8 May 2012

UVa 10469 To Carry or not to Carry Solution

#include <stdio.h>
#include<math.h>
int main()
{
unsigned long num1,num2,num3,k,m,sum;
int rem,parity,l1,l2,i,j,num,d,e;
int s1[100000],s2[100000],s3[100000];

while(scanf("%lu%lu", &num1,&num2)==2)
{
for(k=0;k<100000;k++)
    {
    s1[k]=0;
    s2[k]=0;
    s3[k]=0;
    }



num=num1;
parity=0;
l1=0;
while(num1>0)
 {
 rem = num1%2;
 l1=l1+1;
 if(rem==1)
 parity=parity+1;
 num1 = num1 / 2 ;
 }


for(i=0;i<l1;i++)
     {
    rem = num%2;
     s1[i]=rem;
     num = num / 2 ;
    }
//for(i=l1-1;i>=0;i--)
    //printf("%d",s1[i]);


num=num2;
parity=0;
l2=0;
while(num2>0)
 {
 rem = num2%2;
 l2=l2+1;
 if(rem==1)
 parity=parity+1;
 num2 = num2 / 2 ;
 }


 for(i=0;i<l2;i++)
     {
    rem = num%2;
     s2[i]=rem;
     num = num / 2 ;
    }

if(l1>l2)
k=l1;
else
k=l2;



for(i=k-1;i>=0;i--)
    {
    if((s1[i]+s2[i])==1)
    break;
    }
sum=0;
for(i=i;i>=0;i--)
    {
    if((s1[i]+s2[i])==2)
    e=0;
    else
    e=s1[i]+s2[i];
    if(e==1)
    sum=sum+ pow (2,i);
    }
printf("%lu\n",sum);

}
return 0;
}

No comments:

Post a Comment