您的位置:首页 > 其它

A + B Problem II(大数加法)

2015-07-27 17:01 232 查看

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


[align=left]Problem Description[/align]
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

2
1 2
112233445566778899 998877665544332211

[align=left]Sample Output[/align]

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(100);
b = BigInteger.valueOf(99);
int T;
T = cin.nextInt();//读入T;
//        while(cin.hasNextBigInteger())//判断是否读到文件结尾相当于while(~scanf())
for(int cas = 1; 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 > 1) 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=0;
for(i=s1.length()-1,j=s2.length()-1;i>=0 && j>=0 ;i--,j--)
{
x = s1[i] - '0';
y = s2[j] - '0';
ans += char((x+y+k)%10 + '0');
k = (x+y+k)/10;
}
while(i>=0)
{
x=s1[i]-'0';
ans += char ((x+k)%10 + '0');
k = (x+k)/10;
i--;
}
while(j>=0)
{
y=s2[j]-'0';
ans += char((y+k)%10 + '0');
k = (y+k)/10;
j--;
}
if(k>0)
ans += '1';
//ans.reverse();
reverse(ans.begin(),ans.end());
return ans;
}
//*******************************

//************加法***************
string add(string s1,string s2)
{
string ans = "";
int i,j,x,y,k=0;
for(i=s1.length()-1,j=s2.length()-1;i>=0 && j>=0 ;i--,j--)
{
x = s1[i] - '0';
y = s2[j] - '0';
ans = char((x+y+k)%10 + '0') + ans;
k = (x+y+k)/10;
}
while(i>=0)
{
x=s1[i]-'0';
ans = char ((x+k)%10 + '0') + ans;//不如+=快,但是可以不用倒序
k = (x+k)/10;
i--;
}
while(j>=0)
{
y=s2[j]-'0';
ans = char((y+k)%10 + '0') + ans;
k = (y+k)/10;
j--;
}
if(k>0)
ans = '1' + 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=0;
for(i=s1.length()-1,j=s2.length()-1;i>=0 && j>=0 ;i--,j--)
{
x = s1[i] - '0';
y = s2[j] - '0';
ans += char((x+y+k)%10 + '0');
k = (x+y+k)/10;
}
while(i>=0)
{
x=s1[i]-'0';
ans += char ((x+k)%10 + '0');
k = (x+k)/10;
i--;
}
while(j>=0)
{
y=s2[j]-'0';
ans += char((y+k)%10 + '0');
k = (y+k)/10;
j--;
}
if(k>0)
ans += '1';
//ans.reverse();
reverse(ans.begin(),ans.end());
return ans;
}
int main()
{
string t , tt;
int T ,c = 0 ;
cin>>T;
while(T--)
{
c++;
cin>>t>>tt;
string ans = add(t,tt);
if(c!=1) cout<<endl;
cout<<"Case "<<c<<":"<<endl;
cout<<t<<" + "<<tt<<" = "<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: