Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1002
A + B Problem II
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 230247 Accepted Submission (s): 44185
Problem Descriptioni has a very simple problem for you. Given integers A and B, your job is to calculate the Sum of A + B.
Inputthe first line of the input contains an integer T (1<=t<=20) which means the number of test cases. Then T lines follow, each line consists of both positive integers, A and B. Notice that the integers is very large, that M EANs should not process them by using 32-bit integer. Assume the length of each integer would not exceed 1000.
Outputfor Each test case, you should output of the lines. The first line was "Case #:", # means the number of the the test case. The second line is the equation "A + b = sum", sum means the result of A + B. Note there is some spaces int the Equati On. Output a blank line between the test cases.
Sample Input21 2112233445566778899 998877665544332211
Sample outputcase 1:1 + 2 = 3Case 2:1.,122,334,455,667,79e,+17 + 998877665544332211 = 1111111111111111110
The main topic: Test instructions is easy to understand, specifically does not explain, mainly to solve the problem of large numbers.
Topic idea: If Java, it can be easily ac. Other small partners can only be solved in the most stupid way. We use a number to store the number upside down, whether it's multiplication or addition, which is the best solution.
The following is a two code attached.
1#include <iostream>2#include <cstdio>3#include <cstring>4 5 using namespacestd;6 7 intMain ()8 {9 Chara[8000],b[8000];Ten intMax8000],nb[8000],sum[8000],pre,flag=1; One intT; Ascanf"%d",&t); - while(t--) - { thememset (SUM,0,sizeof(sum)); -memset (Na,0,sizeof(NA)); -memset (NB,0,sizeof(NB)); -scanf"%s%s", A, b); +Pre=0; - intLena=strlen (a); + intlenb=strlen (b); A for(intI=0; i<lena; i++) atna[lena-1-i]=a[i]-'0'; - for(intj=0; j<lenb; J + +) -nb[lenb-1-j]=b[j]-'0'; - intLenx=lena>lenb?Lena:lenb; - for(intk=0; k<lenx; k++) - { inSum[k]=na[k]+nb[k]+pre/Ten; -Pre=Sum[k]; to } + while(pre>9) - { theSum[lenx]=pre/Ten%Ten; *lenx++; $Pre/=Ten;Panax Notoginseng } -printf ("Case %d:\n", flag++); theprintf ("%s +%s =", A, b); + for(inti=lenx-1; i>=0; i--) A { theprintf ("%d", sum[i]%Ten); + } -printf ("\ n"); $ if(t) $printf ("\ n"); - } - the return 0; -}
Java code.
1Import java.util.*;2Import java.math.*;3 Public classMain {4 Public Static voidMain (string[] args) {5Scanner sc=NewScanner (System.inch);6 intL=sc.nextint ();7 for(intI=1; i<=l;i++){8 if(i!=1) System. out. println ();9 BigInteger A, b;TenA=Sc.nextbiginteger (); Oneb=Sc.nextbiginteger (); ASystem. out. println (" Case"+i+":"); -System. out. println (A +" + "+b+" = "+A.add (b)); - } the } -}
hdu1002 A + B problem II (large number of questions)