1042 -- 二进制计算

来源:互联网 发布:乐视网络电视官网下载 编辑:程序博客网 时间:2024/05/16 09:57

二进制计算

Time Limit:1000MS  Memory Limit:65536K
Total Submit:71 Accepted:39

Description

计算两个二进制数的和或差。

Input

输入由两个二进制数和一个运算符组成,二进制数和运算符之间用一个空格分隔,格式如下:

num1 op num2

其中num1和num2为要参与运算的二进制数,二进制数只可能是大于零的无符号整数,且num1>=num2,op为运算符,运算符只可能取+或-;当num1和num2的长度不同时,在长度短的数的左侧补零,比如:

1000 - 1

将被视为

1000 - 0001

Output

运算结果,不能有多余的零。

Sample Input

1011 + 1

Sample Output

1100

Source

    using System;    using System.Collections.Generic;    using System.Linq;    using System.Text;    namespace AK1042 {        class Program {            static long f(long a)//2进制转换成10进制            {                long s = 0, j = 1;                while (a > 0) {                    s += (a % 10) * j;                    a = a / 10;                    j = j * 2;                }                return s;            }            static long g(long s)//10进制转换成2进制            {                long j = 1, n = 0;                while (s > 0) {                    n += (s % 2) * j;                    s = s / 2;                    j *= 10;                }                return n;            }            static void Main(string[] args) {                string sb;                while ((sb = Console.ReadLine()) != null) {                    string[] s = sb.Split();                    long a = long.Parse(s[0]), b = long.Parse(s[2]);                    if (s[1] == "+")                        Console.WriteLine(g(f(a) + f(b)));                    else                        Console.WriteLine(g(f(a) - f(b)));                }            }        }    }


0 0