高精度模拟
#include <stdio.h>
#include <string.h>
int n;
char num1[100], num2[100],num[100];
void turn(char *str, int len)
{
int mid=len>>1;
char temp;
for ( int i= 0; i < mid; i++ )
{
temp= str[i];
str[i]=str[len-1-i];
str[len-1-i]= temp;
}
}
int calu(int len1, int len2)
{
int max, min;
char *str;
if (len1 > len2)
{
max=len1;
min=len2;
str=num1;
}
else
{
max= len2;
min= len1;
str=num2;
}
int i;
memset(num, 0, sizeof(num));
for ( i = 0 ; i < min; i++ )
{
num[i]+=num1[i]+num2[i];
if (num[i]>1)
{
num[i+1]= num[i]/2;
num[i]%=2;
}
}
for ( i = min; i < max; i++ )
{
num[i]+=str[i];
if (num[i]>1)
{
num[i+1]= num[i]/2;
num[i]%=2;
}
}
if (num[i]) return i;
else
for ( i=i-1; i >0; i-- )
if (num[i]) return i;
return 0;
}
int main()
{
scanf("%d", &n);
int l;
for ( l = 1 ; l <= n ; l++ )
{
scanf("%s%s", num1, num2);
printf("%d ", l);
int len1= strlen(num1), len2= strlen(num2);
int i;
for ( i = 0 ; i < len1; i++ )
num1[i]-='0';
for ( i = 0 ; i < len2; i++ )
num2[i]-='0';
turn(num1, len1);
turn(num2, len2);
int len= calu(len1, len2);
for ( i = len ; i >= 0; i-- )
printf("%d", num[i]);
putchar(10);
}
return 0;
}