0
100000
1111 讲道理就是一个大数,不过是个二进制的大数,把十进制的大数模板改改就好了,然而有前导0。。。因为这个前导0我改模板改蒙逼了都,不过还是改过来了 #include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1005;
char a[maxn],b[maxn];
int c[maxn];
int main()
{
while(~scanf("%s",&a))
{
scanf("%s",&b);
int len_a = strlen(a);
int len_b = strlen(b);
int temp = 0;
int cnt = 0;
if(len_a >= len_b)
{
for(int i=len_b-1;i>=0;i--)
{
c[cnt] = ((a[len_a - cnt - 1] - '0') + (b[i] - '0') + temp) % 2;
temp = ((a[len_a - cnt - 1] - '0') + (b[i] - '0') + temp) / 2;
cnt++;
}
for(int i=len_a-len_b-1;i>=0;i--)
{
c[cnt] = ((a[i] - '0')+ temp) % 2;
temp = ((a[i] - '0') + temp) / 2;
cnt++;
}
}
else
{
for(int i=len_a-1;i>=0;i--)
{
c[cnt] = ((b[len_b - cnt - 1] - '0') + (a[i] - '0') + temp) % 2;
temp = ((b[len_b - cnt - 1] - '0') + (a[i] - '0') + temp) / 2;
cnt++;
}
for(int i=len_b-len_a-1;i>=0;i--)
{
c[cnt] = ((b[i] - '0')+ temp) % 2;
temp = ((b[i] - '0') + temp) / 2;
cnt++;
}
}
int flag = 0;
if(!temp)
for(int i=cnt-1;i>=0;i--)
{
if(c[i])
break;
else
flag++;
}
int i;
if(temp)
i=cnt-1;
else
i=cnt-1-flag;
if(flag == max(len_a,len_b))
printf("0");
else
{
if(temp)
printf("%d",temp);
for(;i>=0;i--)
printf("%d",c[i]);
}
printf("\n");
}
return 0;
}
|