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 ;
}
 

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

  1. A + B Problem II 大数加法

    题目描述: Input The first line of the input contains an integer T(1<=T<=20) which means the number ...

  2. hdu1002 A + B Problem II[大数加法]

    目录 题目地址 题干 代码和解释 参考 题目地址 hdu1002 题干 代码和解释 由题意这是一个涉及到大数的加法问题.去看了一眼大数加法的方法感觉头很大,然后突然发现Java可以流氓解决大数问题,毅 ...

  3. A + B Problem II(大数加法)

    一直格式错误,不想改了,没A #include <iostream> #include <stdio.h> #include <string.h> #include ...

  4. hdu1002 A + B Problem II(大数题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java/ ...

  5. 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) ...

  6. 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过

    杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...

  7. HDU 1002 A + B Problem II(高精度加法(C++/Java))

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. hdu1002 A + B Problem II(高精度加法) 2016-05-19 12:00 106人阅读 评论(0) 收藏

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. HDU1002 A + B Problem II 大数问题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java ...

  10. HDU 1023 Train Problem II 大数打表Catalan数

    一个出栈有多少种顺序的问题.一般都知道是Catalan数了. 问题是这个Catalan数非常大,故此须要使用高精度计算. 并且打表会速度快非常多.打表公式要熟记: Catalan数公式 Cn=C(2n ...

随机推荐

  1. SQL Server AlwaysOn添加监听器失败

    标签:MSSQL/ 一.错误描述 1.群集服务未能使群集服务或应用程序“Alwayson22”完全联机或脱机.一个或多个资源可能处于失败状态.这可能会影响群集服务或应用程序的可用性 2.群集服务中的群 ...

  2. Who Will Win?

    Gautam and Subhash are two brothers. They are similar to each other in all respects except one. They ...

  3. IT服务(运维)管理实施的几个要点--序言

    IT服务(运维)管理(不是IT运维技术)是IT行业当中相对比较"窄"的一个分支,通常只被金融.电信等大型数据中心的中高层管理人员所关注.但是根据笔者多年从事IT服务和服务管理的经验 ...

  4. MySQL 字符集问题及安全的更新操作

    一.字符集乱码 1.操作系统字符集 [root@mysql5 ~]# cat /etc/system-release /etc/sysconfig/i18n CentOS release 6.5 (F ...

  5. github emoji 表情列表

    最新emoji大全:emoji列表 emoji-list emoji表情列表 目录 人物 自然 事物 地点 符号 人物 :bowtie: :bowtie:

  6. 用Python玩转微信(一)

    欢迎大家访问我的个人网站<刘江的博客和教程>:www.liujiangblog.com 主要分享Python 及Django教程以及相关的博客 交流QQ群:453131687 今天偶然看见 ...

  7. POI 导出导入工具类介绍

    介绍: Apache POI是Apache软件基金会的开源项目,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. .NET的开发人员则可以利用NPOI (POI ...

  8. C#设计模式之二十三解释器模式(Interpreter Pattern)【行为型】

    一.引言   今天我们开始讲"行为型"设计模式的第十一个模式,也是面向对象设计模式的最后一个模式,先要说明一下,其实这个模式不是最后一个模式(按Gof的排序来讲),为什么把它放在最 ...

  9. 常见ie css hack

    .all IE{property:value\9;} .gte IE 8{property:value\0;} .lte IE 7{*property:value;} .IE 8/9{property ...

  10. mycat安装与配置

    1.安装jdk 测试jdk是否已经安装 [root@node002 ~]# java -version-bash: java: command not found 创建解压目录 [root@node0 ...