A + B Problem II(大数加法)

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 }
时间: 2024-10-11 05:20:35

A + B Problem II(大数加法)的相关文章

A + B Problem II(大数加法)

一直格式错误,不想改了,没A 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <stdlib.h> 5 using namespace std; 6 7 int main() 8 { 9 int T,K=0; 10 scanf("%d",&T); 11 char a[1002],b[1002]; 12 int ta[1002],

(hdu step 2.3.1)A + B Problem II(大数加法)

题目: A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2372 Accepted Submission(s): 917   Problem Description I have a very simple problem for you. Given two integers A and B, your j

hdu1002 A + B Problem II(大数题)

题目链接: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 Description I have a very sim

HDU1002 -A + B Problem II(大数a+b)

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 315214    Accepted Submission(s): 61139 Problem Description I have a very simple problem for you. Given two integers A and B, you

HDU1002 A + B Problem II 大数问题

题目链接: 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): 409136    Accepted Submission(s): 79277 Problem Description I have a very si

HDU 1023 Train Problem II 大数打表Catalan数

一个出栈有多少种顺序的问题.一般都知道是Catalan数了. 问题是这个Catalan数非常大,故此须要使用高精度计算. 并且打表会速度快非常多.打表公式要熟记: Catalan数公式 Cn=C(2n,n) / (n+1); 递推公式 C(n ) = C(n-1)*(4*n-2) / (n+1) 高精度乘以一个整数和高精度除以一个整数的知识.这样还是使用整数数组比較好计算,假设使用string那么就不太好计算了,由于整数也可能是多位的. const int MAX_N = 101; short

抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)

数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. 1 while (a) //将每位数字取出来,取完为止 2 { 3 num1[i]=a%10; //将每一个各位取出存在数组里面,实现了将数字反转 4 i++; //数组的变化 5 a/=10; 6 } 趁热打铁 例题:hdu 4554 叛逆的小明 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid

题解报告:hdu 1002 A + B Problem II(大数加法)

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 fol

hdoj 1002 A + B Problem II【大数加法】

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 260585    Accepted Submission(s): 50389 Problem Description I have a very simple problem for you. Given two integers A and B, you