1201. 01000001
来源:互联网 发布:c语言函数的声明 编辑:程序博客网 时间:2024/06/05 22:37
1201. 01000001
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
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
题目不难,单纯的利用数组存储二进制位串,但是奇葩的事情是为什么我用c语言写的大数相加的代码修改之后尽然不行。
// Problem#: 1201
// Submission#: 1976660
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <string>
using namespace std;
int main(){
int n,i,co;
cin>>n;
for(co=1;co<=n;co++){
string a,b;
cin>>a>>b;
int first[80]={0},second[80]={0},result[81]={0};
while(a.size()<80){
a='0'+a;
}
while(b.size()<80){
b='0'+b;
}
for(i=79;i>=0;i--){
first[i]=a[i]-'0';
second[i]=b[i]-'0';
}
for(i=79;i>=0;i--){
result[i+1]+=(first[i]+second[i]);
if(result[i+1]>=2){
result[i]+=(result[i+1]/2);
result[i+1]%=2;
}
}
cout<<co<<" ";
for(i=0;i<81;i++){
if(result[i]!=0){
break;
}
}
if(i>=81){
cout<<"0";
}
int k;
for(k=i;k<81;k++){
cout<<result[k];
}
cout<<endl;
}
return 0;
}
- 1201. 01000001
- 1201. 01000001
- 1201. 01000001
- Sicily 1201. 01000001
- sicily--1201. 01000001
- Sicily 1201. 01000001
- sicily 1201. 01000001
- 1201. 01000001(纯粹加法操作 )
- sicily1201 01000001
- poj2845 01000001
- fjnu 1382 01000001
- sicily 1201 01000001
- poj 2845 01000001
- POJ 2845 01000001
- Sicily1201——01000001
- POJ 2845 01000001 可能会
- Sicily 1201 01000001(高精度运算)
- POJ 2845 01000001(高精度)
- 使用Java正则表达式来提取一篇文章中的邮箱账号
- Hdu 4504 威威猫系列——篮球梦(DP)
- c#的循环语句之for
- 使用VS2010 C#开发ActiveX控件
- OSI和TCP/IP
- 1201. 01000001
- Android消息处理机制
- JAVA内存管理1
- lenovo g580使用u盘安装64位centos
- 1000.A-B
- Struts2 ognl中的#、%和$符号用法说明(转)
- 单源最短路径的基本算法 -- 算法导论笔记
- 用SDK写基于对话框的程序(不用向导)
- 大数相加,两个大数。