1 Import Staticjava.lang.System.out;2 ImportJava.math.BigInteger;3 ImportJava.util.Scanner;4 5 Public classMain {6 7 Public Static voidMain (string[] args) {8 BigInteger A, b;9Scanner sc =NewScanner (system.in);Ten intT =sc.nextint (); One for(inti = 1; I <= T; i++) { AA =Sc.nextbiginteger (); -b =Sc.nextbiginteger (); -OUT.PRINTLN ("case" + i + ":"); theOut.println (A + "+" + B + "=" +A.add (b)); - if(I <T) - out.println (); - } + } -}
View Code
Faster in Java
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5 #defineSC (x) scanf ("%d", &x)6 #definePF (x) printf ("%d\n", X)7 #definePF (x) printf ("%d", X)8 #defineCL (x, y) memset (x,y,sizeof (×))9 using namespacestd;Ten Const intMAX =1005; One CharA[max], B[max]; A intALen, Blen, N; - intT, K, I, J; - voidADD (CharAChar*b); the intMain () - { - SC (T); - for(i =1; I <= T; i++) + { -CL (A,0); +CL (b,0); ACin >> a >>b; atprintf"Case %d:\n", i); -cout << a <<" + "<< b <<" = "; - Add (A, b); - if(I < T) printf ("\ n"); - } - return 0; in } - voidADD (CharAChar*b) to { +ALen =strlen (a); -Blen =strlen (b); then = MAX-(ALen > Blen? alen:blen);//set the contour line * for(j = alen-1, k = MAX; J >=0; --j,--k) $ {Panax NotoginsengA[k] = a[j]-'0'; -A[J] =0; the } + for(j = blen-1, k = MAX; J >=0; --j,--k) A { theB[k] = b[j]-'0'; +B[J] =0; - } $ for(k = MAX; k >= N;--k) $A[k] + =B[k]; - for(k = MAX; k >= N;--k) - { the inttemp = a[k]/Ten;//Rounding -a[k-1] +=temp;WuyiA[K]%=Ten; the } - for(k = n+1; K <= MAX; k++) Wu //cout << a[k] << Endl; - PF (A[k]); Aboutcout <<Endl; $}
View Code
HDU A + B problem II Big Data