"Hdoj 1002" large number addition

Source: Internet
Author: User

Title: Portal

Solution: large number (range more than can be expressed precision) addition, C + + does not have the corresponding large number of Java classes, directly with the string simulation. Several small trick:

    1. Considering the length of the two numbers, you can change the order of the two in just the beginning (the addition Exchange law), making the first one a longer number;
    2. Characters converted to numbers: Number = S-' 0 ';
    3. Numbers converted to characters: s = (char) (number + ' 0 ');
    4. It is not clear at first that the result is a number of digits, which can be simulated in reverse and then reversed in reverse.
#include <iostream> #include <string> #include <stdio.h> #include <stdlib.h> #include <    Vector>using namespace Std;int main () {int t;    cin>>t;    String PA, Pb, A, B, C, TMP;    Vector<char> Save;    int Jinwei, sum;        for (int k = 0; k < T; k++) {cin>>pa>>pb;        Save.clear ();        C.clear (); Reset Order!            But retain the original value if (Pa.size () < Pb.size ()) {a = PB;        b = PA;            } else {a = PA;        b = PB;        } int maxlen = A.size ();        int minlen = B.size ();        sum = 0;        Jinwei = 0;        int i = MaxLen;        int j = Minlen;            while ((i--) && (j--)) {sum = a[i]-' 0 ' + b[j]-' 0 ';            if (Jinwei) sum++;            if (Sum > 9) {jinwei = 1;            } else {Jinwei = 0; } save.push_back ((char) (sum% 10 + ' 0 '));            }/*if ((maxlen-minlen)! = 0) {*/for (i = (maxlen-minlen-1); I >= 0; i--)                {sum = a[i]-' 0 ';                if (Jinwei) sum++;                if (Sum > 9) {jinwei = 1;                } else {Jinwei = 0;            } save.push_back ((char) (sum% 10 + ' 0 '));        }//} if (Jinwei) {save.push_back (' 1 ');        } for (int i = Save.size ()-1; i>=0; i--) {c = c + save[i];        } printf ("Case%d:\n", k+1);        cout<<pa<< "+" <<pb<< "=" <<c<<endl;        if (k! = t-1) {cout<<endl; }} return 0;}

"Hdoj 1002" large number addition

Related Article

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.