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): 261608 Accepted Submission(s): 50625
Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The 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 two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
题解,大数加法一般可以用java,或者用string 或者用数组手动模拟算法,用c++的时候最好用模板
现在先给出java大数加法的代码
1 import java.math.BigInteger; 2 import java.util.Scanner; 3 4 5 public class Main { 6 public static void main(String[] args) { 7 Scanner cin = new Scanner(System.in);//大数的输入,定义一个输入器 8 BigInteger a = null, b = null, c = null;//开始要赋值成空 9 a = BigInteger.valueOf(100); 10 b = BigInteger.valueOf(99); 11 int T; 12 T = cin.nextInt();//读入T; 13 // while(cin.hasNextBigInteger())//判断是否读到文件结尾相当于while(~scanf()) 14 for(int cas = 1; cas <= T; cas++) 15 { 16 a = cin.nextBigInteger(); 17 b = cin.nextBigInteger(); 18 19 // BigInteger zero = BigInteger.valueOf(0);//大数判断是不是等于0 20 // if(a.equals(BigInteger.valueOf(0))){System.out.println("haha");} 21 // if(a.equals(zero)) {System.out.println("hehe");} 22 c = a.add(b); 23 if(cas > 1) System.out.println();//大数的换行输出 24 System.out.println("Case " + cas + ":");//大数的输出是用+号连接 25 System.out.println(a + " + " + b + " = "+c); 26 } 27 cin.close();//关闭读入器 28 } 29 30 }
下面是大数string模拟的模板
1 //***********加法********************* 2 #include<algorithm> 3 string add(string s1,string s2) 4 { 5 string ans = ""; 6 int i,j,x,y,k=0; 7 for(i=s1.length()-1,j=s2.length()-1;i>=0 && j>=0 ;i--,j--) 8 { 9 x = s1[i] - ‘0‘; 10 y = s2[j] - ‘0‘; 11 ans += char((x+y+k)%10 + ‘0‘); 12 k = (x+y+k)/10; 13 } 14 while(i>=0) 15 { 16 x=s1[i]-‘0‘; 17 ans += char ((x+k)%10 + ‘0‘); 18 k = (x+k)/10; 19 i--; 20 } 21 while(j>=0) 22 { 23 y=s2[j]-‘0‘; 24 ans += char((y+k)%10 + ‘0‘); 25 k = (y+k)/10; 26 j--; 27 } 28 if(k>0) 29 ans += ‘1‘; 30 //ans.reverse(); 31 reverse(ans.begin(),ans.end()); 32 return ans; 33 } 34 //******************************* 35 36 //************加法*************** 37 string add(string s1,string s2) 38 { 39 string ans = ""; 40 int i,j,x,y,k=0; 41 for(i=s1.length()-1,j=s2.length()-1;i>=0 && j>=0 ;i--,j--) 42 { 43 x = s1[i] - ‘0‘; 44 y = s2[j] - ‘0‘; 45 ans = char((x+y+k)%10 + ‘0‘) + ans; 46 k = (x+y+k)/10; 47 } 48 while(i>=0) 49 { 50 x=s1[i]-‘0‘; 51 ans = char ((x+k)%10 + ‘0‘) + ans;//不如+=快,但是可以不用倒序 52 k = (x+k)/10; 53 i--; 54 } 55 while(j>=0) 56 { 57 y=s2[j]-‘0‘; 58 ans = char((y+k)%10 + ‘0‘) + ans; 59 k = (y+k)/10; 60 j--; 61 } 62 if(k>0) 63 ans = ‘1‘ + ans; 64 return ans; 65 } 66 //*********************加法**************************************
下面是完整的代码
1 #include<cstdio> 2 #include<iostream> 3 #include<string> 4 #include<cstring> 5 #include<sstream> 6 #include<algorithm> 7 using namespace std; 8 9 string add(string s1,string s2) 10 { 11 string ans = ""; 12 int i,j,x,y,k=0; 13 for(i=s1.length()-1,j=s2.length()-1;i>=0 && j>=0 ;i--,j--) 14 { 15 x = s1[i] - ‘0‘; 16 y = s2[j] - ‘0‘; 17 ans += char((x+y+k)%10 + ‘0‘); 18 k = (x+y+k)/10; 19 } 20 while(i>=0) 21 { 22 x=s1[i]-‘0‘; 23 ans += char ((x+k)%10 + ‘0‘); 24 k = (x+k)/10; 25 i--; 26 } 27 while(j>=0) 28 { 29 y=s2[j]-‘0‘; 30 ans += char((y+k)%10 + ‘0‘); 31 k = (y+k)/10; 32 j--; 33 } 34 if(k>0) 35 ans += ‘1‘; 36 //ans.reverse(); 37 reverse(ans.begin(),ans.end()); 38 return ans; 39 } 40 int main() 41 { 42 string t , tt; 43 int T ,c = 0 ; 44 cin>>T; 45 while(T--) 46 { 47 c++; 48 cin>>t>>tt; 49 string ans = add(t,tt); 50 if(c!=1) cout<<endl; 51 cout<<"Case "<<c<<":"<<endl; 52 cout<<t<<" + "<<tt<<" = "<<ans<<endl; 53 } 54 return 0; 55 }