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. 【python】函数参数关键字索引、参数指定默认值、搜集参数

  2. 【python】列表

    >>> mix = [2,3.4,"abc",'中国',True,['ab',23]]>>> mix[2, 3.4, 'abc', '中国', ...

  3. 串String(2):串的实现(堆分配存储表示法)

    7/27/2017,先占个位,最近事情比较忙,明天敲一波代码,预测在一星期内搞定 9/02/2017,看到这个十分汗颜,八月去美帝学习了,没有抽空补上这一博文,计划这个月开了数据结构课后补上

  4. UITableView的性能优化

    UITableView作为ios中使用最频繁的控件之一,其性能优化也是常常要面对的,尤其是当数据量偏大并且设备性能不足时.本文旨在总结tableview的几个性能优化tips,并且随着认识的深入,本文 ...

  5. 解决author波浪线Spellchecker inspection helps locate typos and misspelling in your code, comments and literals, and fix them in one click

    自从把默认的头注释的author改成自己的名字以后越看越顺眼,但是发现名字下面一直有个波浪线,强迫症简直不能忍. 然后当你把鼠标放上去,再点击提示上的"more",会看到下面的提示 ...

  6. 读Zepto源码之assets模块

    assets 模块是为解决 Safari 移动版加载图片过大过多时崩溃的问题.因为没有处理过这样的场景,所以这部分的代码解释不会太多,为了说明这个问题,我翻译了<How to work arou ...

  7. 2、转载一篇,浅析人脸检测之Haar分类器方法

    转载地址http://www.cnblogs.com/ello/archive/2012/04/28/2475419.html 浅析人脸检测之Haar分类器方法  [补充] 这是我时隔差不多两年后, ...

  8. 保存html上传文件过程中遇到的字节流和字符流问题总结

    java字节流和字符流的区别以及相同 1. 字节流文件本身进行操作,字符流是通过缓存进行操作, 1.1 使用字节流不执行关闭操作 File f =new File("d:/test/test ...

  9. 搭建非域AlwaysOn win2016+SQL2016

    搭建非域AlwaysOn win2016+SQL2016 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www.cnblogs.co ...

  10. (转)top关键字与top表达式(SQLServer)

    SQLServer 中,top也很有用,例如查询部分数据,还可以用表达式.其语法如下: SELECT TOP number|percent column_name(s) FROM table_name ...