首页 > 代码库 > POJ 2756 Autumn is a Genius 大数加减法

POJ 2756 Autumn is a Genius 大数加减法

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,那么就可以不管数位有多大了。

大数加法比较容易,如果是减法那么题目就比较麻烦了。目前还想不到比较简洁的解法,要特殊处理符号,而且本题是两个数都可能是负数,那么就要分开情况讨论了,符号不同,绝对值大小不同都需要不同处理,情况分的好,那么程序就会相对简洁点。

#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 大数加减法