A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 347161    Accepted Submission(s): 67385

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
Author
Ignatius.L
分析:高精度计算,大数相加!模版在博客中已给出,翻翻看,按照模版写就行了,要注意细节,空格的输出,因为这个PE了2次!
下面给出AC代码:
 #include <bits/stdc++.h>
using namespace std;
int main()
{
char a1[],b1[];
int a[],b[],c[];//a,b,c分别存储加数,加数,结果
int x,i,j,n;
int lena,lenb,lenc;
while(scanf("%d",&n)!=EOF)
{
for(j=;j<=n;j++)
{
memset(a,,sizeof(a));//数组a清零
memset(b,,sizeof(b));//数组b清零
memset(c,,sizeof(c));//数组c清零
scanf("%s%s",&a1,&b1);
lena=strlen(a1);
lenb=strlen(b1);
for(i=;i<=lena;i++)
a[lena-i]=a1[i]-'';//将数串a1转化为数组a,并倒序存储
for(i=;i<=lenb;i++)
b[lenb-i]=b1[i]-'';//将数串b1转化为数组a,并倒序存储
x=;//x是进位
lenc=;//lenc表示第几位
while(lenc<=lena||lenc<=lenb)
{
c[lenc]=a[lenc]+b[lenc]+x;//第lenc位相加并加上次的进位
x=c[lenc]/;//向高位进位
c[lenc]%=;//存储第lenc位的值
lenc++;//位置下标变量
}
c[lenc]=x;
if(c[lenc]==)//处理最高进位
lenc--;
printf("Case %d:\n",j);//格式要求吧!学着点
printf("%s + %s = ",a1,b1);//这也是格式要求吧!学着点
for(i=lenc;i>=;i--)
printf("%d",c[i]);
printf("\n");
if(j!=n)//对于2组之间加空行的情况
printf("\n");
}
}
return ;
}

java写法大数,真是有毒!

 import java.math.BigInteger;
import java.util.Scanner; public class Main { /**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
//System.out.println("Hello World!");
Scanner in=new Scanner(System.in);
while(in.hasNextInt())
{
// int []arr=new int[3];
int n;
n=in.nextInt();
for(int i=1;i<=n;i++)
{
BigInteger a,b;
a=in.nextBigInteger();
b=in.nextBigInteger();
if(i<n)
{
System.out.println("Case "+i+":");
System.out.print(a+" + "+b+" = ");
System.out.println(a.add(b));
System.out.println();
}
else
{
System.out.println("Case "+i+":");
System.out.print(a+" + "+b+" = ");
System.out.println(a.add(b));
}
}
}
}
}

HDU 1002 A + B Problem II(高精度加法(C++/Java))的更多相关文章

  1. HDU 1002 - A + B Problem II - [高精度]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 Problem DescriptionI have a very simple problem ...

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

    数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. while (a) //将每位数字取出来, ...

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

  4. hdoj 1002 A + B Problem II 高精度 java

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

  5. HDU 1002 A + B Problem II

    A + B Problem II   Time Limit: 1000MS      Memory Limit: 65536K Total Submissions: 16104    Accepted ...

  6. HDU 1002 A + B Problem II(大整数相加)

    A + B Problem II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

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

    链接:传送门 题意:A + B 高精度,板子题 /************************************************************************* & ...

  8. 大数加法~HDU 1002 A + B Problem II

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1002 题意: 数学题,A+B; 思路,这个数非常大,普通加法一定会超时,所以用大数加法.大数加法的基 ...

  9. hdu 1002 A + B Problem II【大数加法】

    题目链接>>>>>> 题目大意:手动模拟大数加法,从而进行两个大数的加法运算 #include <stdio.h> #include <strin ...

随机推荐

  1. 下篇: php 微商城 基于Thinkphp3.2框架开发

    (12)微信商城 ① 前台模板引入 a.引入微信商城模板的css+js+Images+img+bootstrap b.引入微商城的首页index.html及详情页detail.html页面模板 注意: ...

  2. BSGS离散对数(Baby-Step-Giant-Step)

    BSGS离散对数(Baby-Step-Giant-Step) 题目: 给定\(x,y,p,\)求最小的自然数\(k\)满足\(x^k=y(\mod p)\)\(p\le2^{31}\)(满足一定有答案 ...

  3. ES6 Proxy和Reflect(下)

    construct() construct方法用于拦截new命令. var handler = { construct (target, args) { return new target(...ar ...

  4. JFinal极速开发框架使用笔记

    记录第一次使用JFinal,从简单的框架搭建到增删改查,从自带的方法到正常框架习惯的使用方式. JFinal官网:http://www.jfinal.com/ JFinal 是基于 Java 语言的极 ...

  5. sql经典试题

    1.一道SQL语句面试题,关于group by表内容:2005-05-09 胜2005-05-09 胜2005-05-09 负2005-05-09 负2005-05-10 胜2005-05-10 负2 ...

  6. 关于理解python类的小题

    今天看了python部落翻译的一篇<一道python类的小题>文章,感觉挺有启发性,记录下来: print('A') class Person(object): print('B') de ...

  7. [编织消息框架][JAVA核心技术]异常应用

    QException是项目业务异常基类 按模块划分子类异常,方便定位那块出错 有个来源码属性code作用定位某个功能处理出错逻辑,数字类型节省内存空间,同时减少创建子类的子类 QSocketExcep ...

  8. Git详解之七:自定义Git

    自定义 Git 到目前为止,我阐述了 Git 基本的运作机制和使用方式,介绍了 Git 提供的许多工具来帮助你简单且有效地使用它. 在本章,我将会介绍 Git 的一些重要的配置方法和钩子机制以满足自定 ...

  9. 二:mysql安装配置、主从复制配置详解

    作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/8213723.html 邮箱:moyi@moyib ...

  10. Java Web高级编程(一)

    Servlet 一.创建Servlet类 在Java EE中,Servlet用来接收和响应终端用户的请求.Servlet是所有Web应用程序的核心类,是唯一既可以直接处理和响应用户请求,也可以将处理工 ...