poj2845 01000001

01000001

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10571   Accepted: 3345

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

java

参考代码:

import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
    static public void main(String[] args)
    {
        Scanner cin = new Scanner(new BufferedInputStream(System.in));
        int t = cin.nextInt();
        for (int i = 0; i < t; i++)
        {
            String st1 = cin.next();
            String st2 = cin.next();
            BigInteger a = new BigInteger(st1, 2);
            BigInteger b = new BigInteger(st2, 2);
            System.out.println((i + 1) + " " + a.add(b).toString(2));
        }
    }
}
时间: 2024-08-26 07:23:20

poj2845 01000001的相关文章

初识Python(一)

一.第一个Python语句 创建 hello.py 文件,内容如下: print 'hello,world' 执行 hello.py 文件,即: python hello.py 执行结果如下: python内部执行过程如下: 二.解释器 默认执行上述的hello.py文件,需要明确的指出hello.py脚本由python解释器来执行,即python hello.py. 另外还可在hello.py的文件头部指定解释器,可像执行shell脚本一样执行python脚本,如下: #!/usr/bin/e

操作系统的抽象与虚拟化

为什么需要操作系统? 说抽象之前得先知道CPU以及键盘.鼠标是之间是工作的.CPU最终状态只能进行加减,当我们输入一个简单的字符"A"的时候键盘与CPU都是把字母"A"转化为能够识别的二进制01才能识别.而这一些对于使用者来说都是透明的,让使用者认为当输入一个字母"A"时就是对字母"A"的一个"投影"显示.但实现这个的一切转化都已经由操作系统帮助我们完成,如果没有操作系统,对于一个字母A的输入,可能就是输入

字符编码

因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte),所以,一个字节能表示的最大的整数就是255(二进制11111111=十进制255),如果要表示更大的整数,就必须用更多的字节.比如两个字节可以表示的最大整数是65535,4个字节可以表示的最大整数是4294967295. 由于计算机是美国人发明的,因此,最早只有127个字符被编码到计算机里,也就是大小写英文字母.数字和一些符号,这个编码表被称为ASCII编

【字符编码】彻底理解字符编码

一.前言 在解决昨天的问题时,又引出了很多新的问题,如为什么要进行编码,这些编码的关系如何,如ASCII,IOS-8859-1,GB2312,GBK,Unicode之间的关系,笔者想要彻底理解字符编码背后的故事,遂进行了探索,具体笔记如下.如园友能读完本篇文章,我相信会解开很多疑惑. 二.字符编码 2.1 为何需要编码? 我们知道,所有的信息最终都表示为一个二进制的字符串,每一个二进制位(bit)有0和1两种状态.当我们需要把字符'A'存入计算机时,应该对应哪种状态呢,存储时,我们可以将字符'A

字符编码、文件操作、函数定义

一.字符编码 字符串是一种数据类型,但是,字符串比较特殊的是还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte),所以,一个字节能表示的最大的整数就是255(二进制11111111=十进制255),如果要表示更大的整数,就必须用更多的字节.比如两个字节可以表示的最大整数是65535,4个字节可以表示的最大整数是4294967295. 由于计算机是美国人发明的,因此,最早只有127个字符被编

SQL SERVER大话存储结构(1)

阅读目录(Content) 1 数据页的类型 1.1 PFS 1.2.3 IAM 2 数据页结构 2.1 页头 2.2 行记录 2.3 空闲空间 2.4 行偏移量 3 查询数据页存储格式的途径 3.1 dbcc ind 3.1.1 语法说明 3.1.2 测试案例 3.2 dbcc page 3.2.1 语法说明 3.2.2 测试案例 如果转载,请注明博文来源: www.cnblogs.com/xinysu/   ,版权归 博客园 苏家小萝卜 所有.望各位支持! SQLServer的数据页大小是8

&lt;转&gt;字符编码笔记:ASCII,Unicode和UTF-8

本文转自:http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html 今天中午,我突然想搞清楚Unicode和UTF-8之间的关系,于是就开始在网上查资料. 结果,这个问题比我想象的复杂,从午饭后一直看到晚上9点,才算初步搞清楚. 下面就是我的笔记,主要用来整理自己的思路.但是,我尽量试图写得通俗易懂,希望能对其他朋友有用.毕竟,字符编码是计算机技术的基石,想要熟练使用计算机,就必须懂得一点字符编码的知识. 1. ASC

8、字符编码-Python(转)

一 了解字符编码的知识储备 1. 文本编辑器存取文件的原理(nodepad++,pycharm,word) 打开编辑器就打开了启动了一个进程,是在内存中的,所以在编辑器编写的内容也都是存放与内存中的,断电后数据丢失 因而需要保存到硬盘上,点击保存按钮,就从内存中把数据刷到了硬盘上. 在这一点上,我们编写一个py文件(没有执行),跟编写其他文件没有任何区别,都只是在编写一堆字符而已. 2. python解释器执行py文件的原理 ,例如python test.py 第一阶段:python解释器启动,

字符编码格式

一.编码历史与区别 一直对字符的各种编码方式懵懵懂懂,什么ANSI UNICODE UTF-8 GB2312 GBK DBCS UCS--是不是看的很晕,假如您细细的阅读本文你一定可以清晰的理解他们.Let's Go! 很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态,以表示世界上的万物.他们看到8个开关状态是好的,于是他们把这称为"字节". 再后来,他们又做了一些可以处理这些字节的机器,机器开动了,可以用字节来组合出很多状态,状态开始变来变去.他们看到这样是好