POJ 2756 Autumn is a Genius 大数加减法
Description
Input
Output
Sample Input
1
1 2
Sample Output
3
Hint
本题题目没明白说明有多大的数,主要是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 大数加减法的更多相关文章
- POJ 2756 Autumn is a Genius 采用string大数减法
标题意味着小神童.加减可以计算. 只是说这个小神童的学科知识,究竟有多神,自己给自己找. 最后,因为数据是非常非常巨大的,我听说关闭50k结束了50000数字总和,可以想见他神教. 这似乎也是考试题目 ...
- POJ 2389 Bull Math(水~Java -大数相乘)
题目链接:http://poj.org/problem?id=2389 题目大意: 大数相乘. 解题思路: java BigInteger类解决 o.0 AC Code: import java.ma ...
- poj 2506 Tiling(递推 大数)
题目:http://poj.org/problem?id=2506 题解:f[n]=f[n-2]*2+f[n-1],主要是大数的相加; 以前做过了的 #include<stdio.h> # ...
- POJ 3982 序列(JAVA,简单,大数)
题目 //在主类中 main 方法必须是 public static void 的,在 main 中调用非static类时会有警告信息, //可以先建立对象,然后通过对象调用方法: import ja ...
- POJ 2413 How many Fibs?#二分+大数加法
http://poj.org/problem?id=2413 #include<iostream> #include<cstdio> #include<cstring&g ...
- POJ 2635 The Embarrassed Cryptographer(大数求余)
题意:给出一个大数,这个大数由两个素数相乘得到,让我们判断是否其中一个素数比L要小,如果两个都小,输出较小的那个. 分析:大数求余的方法:针对题目中的样例,143 11,我们可以这样算,1 % 11 ...
- 大数加减法 - java实现
计算机处理的各种数据类型都有个范围,超出范围的就处理不了. 如果做超大数运算加减乘除,普通方法肯定是不行的,那么我们遇到大数的运算怎么处理呢?今天介绍一种大数加减乘除运算的方法 思路: 1. 将两个特 ...
- POJ 3181 Dollar Dayz ( 完全背包 && 大数高精度 )
题意 : 给出目标金额 N ,问你用面额 1~K 拼成 N 的方案有多少种 分析 : 完全背包的裸题,完全背包在 DP 的过程中实际就是列举不同的装填方案数来获取最值的 故状态转移方程为 dp[i] ...
- 从一道NOI练习题说递推和递归
一.递推: 所谓递推,简单理解就是推导数列的通项公式.先举一个简单的例子(另一个NOI练习题,但不是这次要解的问题): 楼梯有n(100 > n > 0)阶台阶,上楼时可以一步上1阶,也可 ...
随机推荐
- qtcreator +vs2013 开发xp下使用的程序
在qtcreator 开发,使用vs2013的编辑器开发出来的exe不能在xp下使用, 只需要在pro文件添加 QMAKE_LFLAGS_WINDOWS = /SUBSYSTEM:WINDOWS,5. ...
- PHP开发经验中介(thinkphp3.2使用技巧)
1.在模板中截取字符串 {$vo.create_date|mb_substr=###,0,10,'utf-8'}
- Linux 安装Redis全过程日志
wget http://download.redis.io/redis-stable.tar.gz tar xvzf redis-stable.tar.gz cd redis-stable make ...
- win2k/xp查看当前进程
win2k/xp查看当前进程 tasklist tasklist | find "关键字" 可以对结果进行过滤 关闭当前某个进程 taskkill /pid 程序的PID号码 wi ...
- VHDL TestBench 测试终止时自动结束仿真——assert方法
可在结束仿真位置添加如下代码: assert false report "Simulation is finished!" severity Failure; 则在Modelsim ...
- Problem B: Ternarian Weights
大致题意:使用三进制砝码采取相应的措施衡量出给定的数字主要思路:三进制,如果 大于 2 向前进位,之前一直没写好放弃了,这次终于写好了…… #include <iostream> #inc ...
- SilkTest Q&A 9
Q81:我应该如何存取excel sheet里面的空字符串? A81:定制代码如下: 解决方案1: [-] if sText!=NULL [ ] Page.tfldName.SetText(sText ...
- Python标准库:内置函数dict(iterable, **kwarg)
本函数是从可迭代对象来创建新字典.比方一个元组组成的列表,或者一个字典对象. 样例: #dict() #以键对方式构造字典 d1 = dict(one = 1, two = 2, a = 3) pri ...
- Java总结之容器
[容器的概念] 容器:Java API所提供的一系列的实例,用于在程序中存放对象. [容器 API] J2SDK所提供的容器API位于java.util包内. {Collection[Set(Hash ...
- 【ASP.NET Web API教程】3.4 HttpClient消息处理器
原文:[ASP.NET Web API教程]3.4 HttpClient消息处理器 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. 3.4 ...