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

11 2

Sample Output

3

Hint

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

The topic does not understand how large the number, mainly a, B < 32768 confuse people, it seems not large number, just behind the size of the input will not exceed 50K this sentence is that the large number of can be close to infinity of negative numbers.

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

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

Large number addition is easier. If it is subtraction then the problem is more troublesome. There is no more concise solution at this moment. To special deal with the symbol, and the subject is two numbers may be negative, then the separate situation is discussed, the symbols are different. The difference in absolute size requires a different treatment. The situation is well divided. 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 '); Etfrombuf ();}} void Addbignum (vector<short> &rs, vector<short> &a, vector<short> &B) {rs.clear (); 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--) {sh Ort a = i >= an? A[i]: 0;short b = J >= bn? B[J]: 0;carry + = a + b;rs.push_back (carry%); carry/= 10;} RevErse (Rs.begin (), Rs.end ());} void Minusbignum (vector<short> &rs, vector<short> &a, vector<short> &B) {rs.clear (); 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 >=?

A[i]: 0;short b = J >= bn? B[J]: 0;if (a > B) {Short sum = A-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 ()) A = 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, Bsig n = 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); bsign) Putchar ('-');} Else{minusbignum (RS, A, B); 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 large number plus subtraction

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.