POJ 2756 Autumn is a Genius large number plus subtraction

Source: Internet
Author: User

Description Jiajia and Wind has a very cute daughter called Autumn. She's so clever, she can do integer additions if she was just 2 years old! Since a lot of people suspect that Autumn could 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 is 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 ' + ' before the non-negative number!

The topic does not clearly indicate how large the number, mainly a, B < 32768 confuse people, as if not a large number, but the following the size of the input will not exceed 50K this sentence is that the large number can be close to infinity negative numbers.

In fact, 50K should be open how large array it. 50 * 1024/8 = = 6400, so there will be 6,400 digits.

Here you can use either C + + vector or string, and then enter buffer, so no matter how large the digits are.

Large number of additions is easier, if it is subtraction then the problem is more troublesome. At present, we can not think of a relatively concise solution, to special processing symbols, and the subject is two numbers may be negative, then it is necessary to separate the situation discussed, the symbol is different, the absolute size of different need different treatment, the situation is good, then the program will be relatively concise point.

#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-carry;
			carry = 0;
		Rs.push_back (sum);
			} else if (a < b) {short sum = ten-(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; }




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.