A + B Problem II(大数加法)

时间:2021-12-21 05:07:09

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大数加法的代码
 import java.math.BigInteger;
import java.util.Scanner; public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);//大数的输入,定义一个输入器
BigInteger a = null, b = null, c = null;//开始要赋值成空
a = BigInteger.valueOf();
b = BigInteger.valueOf();
int T;
T = cin.nextInt();//读入T;
// while(cin.hasNextBigInteger())//判断是否读到文件结尾相当于while(~scanf())
for(int cas = ; cas <= T; cas++)
{
a = cin.nextBigInteger();
b = cin.nextBigInteger(); // BigInteger zero = BigInteger.valueOf(0);//大数判断是不是等于0
// if(a.equals(BigInteger.valueOf(0))){System.out.println("haha");}
// if(a.equals(zero)) {System.out.println("hehe");}
c = a.add(b);
if(cas > ) System.out.println();//大数的换行输出
System.out.println("Case " + cas + ":");//大数的输出是用+号连接
System.out.println(a + " + " + b + " = "+c);
}
cin.close();//关闭读入器
} }

下面是大数string模拟的模板

 //***********加法*********************
#include<algorithm>
string add(string s1,string s2)
{
string ans = "";
int i,j,x,y,k=;
for(i=s1.length()-,j=s2.length()-;i>= && j>= ;i--,j--)
{
x = s1[i] - '';
y = s2[j] - '';
ans += char((x+y+k)% + '');
k = (x+y+k)/;
}
while(i>=)
{
x=s1[i]-'';
ans += char ((x+k)% + '');
k = (x+k)/;
i--;
}
while(j>=)
{
y=s2[j]-'';
ans += char((y+k)% + '');
k = (y+k)/;
j--;
}
if(k>)
ans += '';
//ans.reverse();
reverse(ans.begin(),ans.end());
return ans;
}
//******************************* //************加法***************
string add(string s1,string s2)
{
string ans = "";
int i,j,x,y,k=;
for(i=s1.length()-,j=s2.length()-;i>= && j>= ;i--,j--)
{
x = s1[i] - '';
y = s2[j] - '';
ans = char((x+y+k)% + '') + ans;
k = (x+y+k)/;
}
while(i>=)
{
x=s1[i]-'';
ans = char ((x+k)% + '') + ans;//不如+=快,但是可以不用倒序
k = (x+k)/;
i--;
}
while(j>=)
{
y=s2[j]-'';
ans = char((y+k)% + '') + ans;
k = (y+k)/;
j--;
}
if(k>)
ans = '' + ans;
return ans;
}
//*********************加法**************************************

下面是完整的代码

 #include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<sstream>
#include<algorithm>
using namespace std; string add(string s1,string s2)
{
string ans = "";
int i,j,x,y,k=;
for(i=s1.length()-,j=s2.length()-;i>= && j>= ;i--,j--)
{
x = s1[i] - '';
y = s2[j] - '';
ans += char((x+y+k)% + '');
k = (x+y+k)/;
}
while(i>=)
{
x=s1[i]-'';
ans += char ((x+k)% + '');
k = (x+k)/;
i--;
}
while(j>=)
{
y=s2[j]-'';
ans += char((y+k)% + '');
k = (y+k)/;
j--;
}
if(k>)
ans += '';
//ans.reverse();
reverse(ans.begin(),ans.end());
return ans;
}
int main()
{
string t , tt;
int T ,c = ;
cin>>T;
while(T--)
{
c++;
cin>>t>>tt;
string ans = add(t,tt);
if(c!=) cout<<endl;
cout<<"Case "<<c<<":"<<endl;
cout<<t<<" + "<<tt<<" = "<<ans<<endl;
}
return ;
}