# POJ 2845 01000001 二进制加法 套用高精度加法运算代码

01000001
 Time Limit: 1000MS Memory Limit: 65536K

Description

Adding binary numbers is a very simple task, and very similar to the longhand addition of decimal numbers. As with decimal numbers, you start by adding the bits (digits) one column at a time, from right to left. Unlike decimal addition, there is little to memorize in the way of rules for the addition of binary bits:

   0 + 0 = 0   1 + 0 = 1   0 + 1 = 1   1 + 1 = 10   1 + 1 + 1 = 11

Just as with decimal addition, when the sum in one column is a two-bit (two-digit) number, the least significant figure is written as part of the total sum and the most significant figure is “carried” to the next left column. Consider the following examples:

                       11  1 <-- Carry bits --> 1   11  1001101             1001001                    1000111+ 0010010           + 0011001                  + 1010110 --------           ---------                  ---------  1011111             1100010                   10011101

The addition problem on the left did not require any bits to be carried, since the sum of bits in each column was either 1 or 0, not 10 or 11. In the other two problems, there definitely were bits to be carried, but the process of addition is still quite simple.

Input

The first line of input contains an integer N, (1 ≤ N ≤ 1000), which is the number of binary addition problems that follow. Each problem appears on a single line containing two binary values separated by a single space character. The maximum length of each binary value is 80 bits (binary digits). Note: The maximum length result could be 81 bits (binary digits).

Output

For each binary addition problem, print the problem number, a space, and the binary result of the addition. Extra leading zeroes must be omitted.

Sample Input

3
1001101 10010
1001001 11001
1000111 1010110

Sample Output

1 1011111
2 1100010
3 10011101
高精度加法代码
/* Author : yan
* Question : POJ 2845 01000001
* Data && Time : Monday, January 10 2011 01:00 PM
* Compiler : gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
*/
#include<stdio.h>
#define MAX 83

char result[MAX+1];
{
char a[MAX],b[MAX];
int i,Len,Len1,Len2,k;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(result,0,sizeof(result));
Len1=strlen(s1);
Len2=strlen(s2);
if(Len1>Len2) Len=Len1;
else Len=Len2;
k=0;
for(i=Len1-1;i>=0;i--) a[++k]=s1[i]-'0';
k=0;
for(i=Len2-1;i>=0;i--) b[++k]=s2[i]-'0';
for(i=1;i<=Len;i++)
{
a[i]=a[i]+b[i];
if (a[i]>=2)
{
a[i]=a[i]-2;
a[i+1]++;
}
}
if(a[Len+1]==0) Len--;
k=0;
for(i=Len+1;i>=1;i--) result[k++]=a[i]+'0';
}
int main()
{
//freopen("input","r",stdin);
int test;
int cnt=1;
int index;
scanf("%d",&test);
while(test--)
{
index=0;
printf("%d ",cnt++);
while(result[index]=='0') index++;
if(result[index]=='/0') printf("0");
else
{
while(result[index]!='/0') printf("%c",result[index++]);
}
printf("/n");
}

return 0;
}

WA代码，帮忙看看是哪里的原因
/* Author : yan
* Question : POJ 2845 01000001
* Data && Time : Sunday, January 09 2011 01:20 PM
* Compiler : gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
*/
#include<stdio.h>
//#define long long long long

long long cache1;
long long cache2;
char bits1[85];
char bits2[85];

void binaty_to_decimal(const char *bit,long long *value)
{
int len=strlen(bit);
int i;
for(i=0;i<len;i++) *value+=(bit[i]-'0')*pow(2,len-i-1);
}
void decimal_to_binary(long long value,char *bit)
{
short cnt=0;
if(value==0)
{
bit[0]='0';
bit[1]='/0';
return;
}
while(value)
{
bit[cnt++]=value%2+'0';
//printf("%d",value%2);
value=value/2;
}
int i;
char c;
for(i=0;i<cnt/2;i++)
{
c=bit[i];
bit[i]=bit[cnt-i-1];
bit[cnt-i-1]=c;
}
bit[cnt]='/0';
}
int main()
{
freopen("input","r",stdin);
int test;
int cnt=1;
int index;
scanf("%d",&test);
while(test--)
{
scanf("%s%s",bits1,bits2);
cache1=cache2=0;
binaty_to_decimal(bits1,&cache1);
binaty_to_decimal(bits2,&cache2);
decimal_to_binary(cache1+cache2,bits1);
printf("%d ",cnt++);
index=0;
if(strlen(bits1)==1)
{
printf("%s/n",bits1);
continue;
}
while(bits1[index]=='0') index++;
while(bits1[index]!='/0') printf("%c",bits1[index++]);
printf("/n");
//printf("%d/n",cnt++);
}
return 0;
} 

• 本文已收录于以下专栏：

## 整数高精度运算的库（加法，减法，乘法，除法，取模）

• hiiiiiu
• 2011年05月28日 18:26
• 2416

## 用C++实现高精度加法运算

• highyon
• 2016年03月31日 09:56
• 6842

## HDU 1002 (高精度加法运算）

A + B ProblemII Time Limit: 2000/1000 MS(Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T...

## POJ 1503(高精度整数加法) 解题报告

POJ 1503(高精度整数加法) 解题报告
• masikkk
• 2011年03月18日 00:15
• 1354

## POJ 3181 Dollar Dayz(完全背包+简单高精度加法)

POJ 3181 Dollar Dayz(完全背包+简单高精度加法) http://poj.org/problem?id=3181 题意:        给你K种硬币,每种硬币分别是1美元,2美...

## POJ 1504 Adding Reversed Numbers（我的水题之路——逆向数高精度加法）

Adding Reversed Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:...

## C语言-高精度加法

• 2014年11月13日 09:41
• 686B
• 下载

## 高精度加法的C++实现

• 2010年12月06日 18:51
• 936B
• 下载

举报原因： 您举报文章：POJ 2845 01000001 二进制加法 套用高精度加法运算代码 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)