Problem Type : Math
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
int i,j,k,n,m,l,len1,len2,len,x,count;
char a[100],b[100],sum[100];
while(scanf("%d%d",&n,&m)==2)
{
for(i=0; ;i++)
{
l=n%2;
a[i]=l+48;
if(n==0)
break;
n=n/2;
}
a[i]='\0';
for(j=0; ;j++)
{
l=m%2;
b[j]=l+48;
if(m==0)
break;
m=m/2;
}
b[j]='\0';
len1=strlen(a);
len2=strlen(b);
reverse (a,a+len1);
reverse(b,b+len2);
if(len1<len2)
{
reverse (a,a+len1);
for(i=len1;i<len2;i++)
{
a[i]=0+48;
}
a[i]='\0';
reverse(a,a+len2);
}
if(len1>len2)
{
reverse (b,b+len2);
for(i=len2;i<len1;i++)
{
b[i]=0+48;
}
b[i]='\0';
reverse(b,b+len1);
}
len=strlen(a);
for(i=0;i<len;i++)
{
if(a[i]=='0'&& b[i]=='0')
sum[i]='0';
else if(a[i]=='0'&& b[i]=='1')
sum[i]='1';
else if(a[i]=='1'&& b[i]=='0')
sum[i]='1';
else if(a[i]=='1'&& b[i]=='1')
sum[i]='0';
}
sum[len]='\0';
count=0;
for(i=len-1,j=0;i>=0;i--,j++)
{
x=sum[i]-48;
count += x * pow(2,j);
}
printf("%d\n",count);
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
int i,j,k,n,m,l,len1,len2,len,x,count;
char a[100],b[100],sum[100];
while(scanf("%d%d",&n,&m)==2)
{
for(i=0; ;i++)
{
l=n%2;
a[i]=l+48;
if(n==0)
break;
n=n/2;
}
a[i]='\0';
for(j=0; ;j++)
{
l=m%2;
b[j]=l+48;
if(m==0)
break;
m=m/2;
}
b[j]='\0';
len1=strlen(a);
len2=strlen(b);
reverse (a,a+len1);
reverse(b,b+len2);
if(len1<len2)
{
reverse (a,a+len1);
for(i=len1;i<len2;i++)
{
a[i]=0+48;
}
a[i]='\0';
reverse(a,a+len2);
}
if(len1>len2)
{
reverse (b,b+len2);
for(i=len2;i<len1;i++)
{
b[i]=0+48;
}
b[i]='\0';
reverse(b,b+len1);
}
len=strlen(a);
for(i=0;i<len;i++)
{
if(a[i]=='0'&& b[i]=='0')
sum[i]='0';
else if(a[i]=='0'&& b[i]=='1')
sum[i]='1';
else if(a[i]=='1'&& b[i]=='0')
sum[i]='1';
else if(a[i]=='1'&& b[i]=='1')
sum[i]='0';
}
sum[len]='\0';
count=0;
for(i=len-1,j=0;i>=0;i--,j++)
{
x=sum[i]-48;
count += x * pow(2,j);
}
printf("%d\n",count);
}
return 0;
}
No comments:
Post a Comment