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:
- 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;
- Characters converted to numbers: Number = S-' 0 ';
- Numbers converted to characters: s = (char) (number + ' 0 ');
- 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