Description

Jiajia and Wind have a very cute daughter called Autumn. She is so clever that she can do integer additions when she was just 2 years old! Since a lot of people suspect that Autumn may make mistakes, please write a program to prove that Autumn is a real genius.

Input

The first line contains a single integer T, the number of test cases. The following lines contain 2 integers A, B(A, B < 32768) each. The size of input will not exceed 50K.

Output

The output should contain T lines, each with a single integer, representing the corresponding sum.

Sample Input

1
1 2

Sample Output

3

Hint

There may be '+' before the non-negative number!

本题题目没明白说明有多大的数,主要是A, B < 32768迷惑人,好像不是大数,只是后面 The size of input will not exceed 50K 的这句话就说明是大数了能够为接近无穷大的负数。

事实上50K就应该开多大的数组呢?50 * 1024 / 8 == 6400,所以会有6400个数位。

这里直接使用C++的vector或者string,然后输入使用buffer,那么就能够无论数位有多大了。

大数加法比較easy。假设是减法那么题目就比較麻烦了。眼下还想不到比較简洁的解法。要特殊处理符号,并且本题是两个数都可能是负数,那么就要分开情况讨论了,符号不同。绝对值大小不同都须要不同处理。情况分的好。那么程序就会相对简洁点。

#include <stdio.h>
#include <string>
#include <algorithm>
#include <vector>
using namespace std; const int MAX_BUF = 512;
int id = 0, len = 0;
char buf[MAX_BUF]; char getFromBuf()
{
if (id >= len)
{
len = fread(buf, 1, MAX_BUF, stdin);
id = 0;
}
return buf[id++];
} void getNum(vector<short> &num)
{
char c = getFromBuf();
while (('\n' == c || ' ' == c) && len) c = getFromBuf();
while ('\n' != c && ' ' != c && len)
{
num.push_back(c-'0');
c = getFromBuf();
}
} void addBigNum(vector<short> &rs, vector<short> &A, vector<short> &B)
{
rs.clear();
if (A.empty())
{
rs = B;
return;
}
if (B.empty())
{
rs = A;
return;
}
int an = A[0] == '+'-'0' || A[0] == '-'-'0'? 1:0;
int bn = B[0] == '+'-'0' || B[0] == '-'-'0'? 1:0;
short carry = 0;
int i = (int)A.size()-1;
int j = (int)B.size()-1;
for (; i >= an || j >= bn || carry; i--, j--)
{
short a = i >= an? A[i] : 0;
short b = j >= bn? B[j] : 0;
carry += a + b;
rs.push_back(carry % 10);
carry /= 10;
}
reverse(rs.begin(), rs.end());
} void minusBigNum(vector<short> &rs, vector<short> &A, vector<short> &B)
{
rs.clear();
if (B.empty())
{
if (A.empty()) return;
int i = A[0] == '-'-'0' || A[0] == '+'-'0'? 1 : 0;
for (; i < (int)A.size(); i++) rs.push_back(A[i]);
return ;
}
int an = A[0] == '-'-'0' || A[0] == '+'-'0' ? 1 : 0;
int bn = B[0] == '-'-'0' || B[0] == '+'-'0' ? 1 : 0; short carry = 0;
int i = (int)A.size() - 1;
int j = (int)B.size() - 1;
for (; i >= an || j >= bn || carry != 0; i--, j--)
{
short a = i >= an ? A[i] : 0;
short b = j >= bn ? B[j] : 0;
if (a > b)
{
short sum = a - b - carry;
carry = 0;
rs.push_back(sum);
}
else if (a < b)
{
short sum = 10 - (b - a) - carry;
carry = 1;
rs.push_back(sum);
}
else
{
short sum = 0;
if (carry) sum = 9;
rs.push_back(sum);
}
}
reverse(rs.begin(), rs.end());
} int cmp(vector<short> &A, vector<short> &B)
{
int an, bn;
if (A.empty()) an = 0;
else an = A[0] == '-'-'0' || A[0] == '+'-'0'? A.size()-1 : A.size();
if (B.empty()) bn = 0;
else bn = B[0] == '-'-'0' || B[0] == '+'-'0'? B.size()-1 : B.size(); if (an > bn) return 1;
if (an < bn) return -1;
if (!an) return 0; int i = A[0] == '-'-'0' || A[0] == '+'-'0'? 1 : 0;
int j = B[0] == '-'-'0' || B[0] == '+'-'0'? 1 : 0; int res = 0;
for (; i < (int)A.size(); i++, j++)
{
if (A[i] < B[j]) res = -1;
else if (A[i] > B[j]) res = 1;
if (res != 0) break;
}
return res;
} int main()
{
int T;
scanf("%d", &T);
while (T--)
{
vector<short> A, B, rs;
getNum(A);
getNum(B); bool Asign = true, Bsign = true;
if (!A.empty() && A[0] == '-'-'0') Asign = false;
if (!B.empty() && B[0] == '-'-'0') Bsign = false;
if (Asign == Bsign)
{
addBigNum(rs, A, B);
if (!Asign) putchar('-');
}
else
{
int c = cmp(A, B);
if (0 == c) rs.push_back(0);
else if (c < 0)
{
minusBigNum(rs, B, A);
if (!Bsign) putchar('-');
}
else
{
minusBigNum(rs, A, B);
if (!Asign) putchar('-');
}
}
for (int i = 0; i < (int)rs.size(); i++)
{
printf("%d", rs[i]);
}
putchar('\n');
}
return 0;
}

POJ 2756 Autumn is a Genius 大数加减法的更多相关文章

  1. POJ 2756 Autumn is a Genius 采用string大数减法

    标题意味着小神童.加减可以计算. 只是说这个小神童的学科知识,究竟有多神,自己给自己找. 最后,因为数据是非常非常巨大的,我听说关闭50k结束了50000数字总和,可以想见他神教. 这似乎也是考试题目 ...

  2. POJ 2389 Bull Math(水~Java -大数相乘)

    题目链接:http://poj.org/problem?id=2389 题目大意: 大数相乘. 解题思路: java BigInteger类解决 o.0 AC Code: import java.ma ...

  3. poj 2506 Tiling(递推 大数)

    题目:http://poj.org/problem?id=2506 题解:f[n]=f[n-2]*2+f[n-1],主要是大数的相加; 以前做过了的 #include<stdio.h> # ...

  4. POJ 3982 序列(JAVA,简单,大数)

    题目 //在主类中 main 方法必须是 public static void 的,在 main 中调用非static类时会有警告信息, //可以先建立对象,然后通过对象调用方法: import ja ...

  5. POJ 2413 How many Fibs?#二分+大数加法

    http://poj.org/problem?id=2413 #include<iostream> #include<cstdio> #include<cstring&g ...

  6. POJ 2635 The Embarrassed Cryptographer(大数求余)

    题意:给出一个大数,这个大数由两个素数相乘得到,让我们判断是否其中一个素数比L要小,如果两个都小,输出较小的那个. 分析:大数求余的方法:针对题目中的样例,143 11,我们可以这样算,1 % 11 ...

  7. 大数加减法 - java实现

    计算机处理的各种数据类型都有个范围,超出范围的就处理不了. 如果做超大数运算加减乘除,普通方法肯定是不行的,那么我们遇到大数的运算怎么处理呢?今天介绍一种大数加减乘除运算的方法 思路: 1. 将两个特 ...

  8. POJ 3181 Dollar Dayz ( 完全背包 && 大数高精度 )

    题意 : 给出目标金额 N ,问你用面额 1~K 拼成 N 的方案有多少种 分析 : 完全背包的裸题,完全背包在 DP 的过程中实际就是列举不同的装填方案数来获取最值的 故状态转移方程为 dp[i] ...

  9. 从一道NOI练习题说递推和递归

    一.递推: 所谓递推,简单理解就是推导数列的通项公式.先举一个简单的例子(另一个NOI练习题,但不是这次要解的问题): 楼梯有n(100 > n > 0)阶台阶,上楼时可以一步上1阶,也可 ...

随机推荐

  1. ASP.NET成员资格与角色管理配置内容

    Web.config中进行配置 以便于连接数据库,使用微软提供的Membership类.·····等   <?xml version="1.0" encoding=" ...

  2. Silverlight技术调查(1)——Html向Silverlight传参

    原文 Silverlight技术调查(1)——Html向Silverlight传参 近几日项目研究一个很牛的富文档编辑器DXperience RichEdit组件,调查环境为Silverlight4. ...

  3. HTTP协议和web工作原理

    本章学完之后能干什么? 要把 知识点学好,那就需要把它相关的周边知识点了解全面 HTTP协议是web学习的核心!!! 学东东切忌只学配置,不学原理:只学会框架有什么用,要会自己写框架!! web学习直 ...

  4. linux内核系统调用--sendfile函数

    在apache,nginx,lighttpd等webserver其中,都有一项sendfile相关的配置,在一些网上的资料都有谈到sendfile会提升文件传输性能,那sendfile究竟是什么呢?它 ...

  5. SPARK在linux中的部署,以及SPARK中聚类算法的使用

    眼下,SPARK在大数据处理领域十分流行.尤其是对于大规模数据集上的机器学习算法.SPARK更具有优势.一下初步介绍SPARK在linux中的部署与使用,以及当中聚类算法的实现. 在官网http:// ...

  6. 一些关于Console的API函数

    SetConsoleCtrlHandlerGenerateConsoleCtrlEventSetConsoleMode ReadConsole WriteConsole SetConsoleCP Se ...

  7. 手机字段存储报错 :Warning Code : 1264 Out of range value for column 'buyer_tpl' at row 1

    企鹅上朋友问我: 我这明明是11位的int 为啥还说超出范围了呢,然后发来报警截图 我看到是 buyer_tpl int(13)  unsigned NOT NULL,就知道是怎么回事了,打开dev. ...

  8. Maven插件之portable-config-maven-plugin(不同环境打包)

    在大型的项目组中,分不同的开发环境,测试环境,生产环境(说白了就是配置文件不同,或者数据源,或者服务器,或者数据库等);问题来了,如何使用Maven针对不同的环境来打包呢? Maven提供了Profi ...

  9. Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations

    D. Xenia and Bit Operations time limit per test 2 seconds memory limit per test 256 megabytes input ...

  10. uva 11732 - strcmp() Anyone? 不错的Trie题

    题解:http://blog.csdn.net/u013480600/article/details/23122503 我的代码一直TLE,,,看了人家的之后,认为1.链式前向星比較好,2.*dept ...