博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1002 A + B Problem II(高精度加法(C++/Java))
阅读量:6806 次
发布时间:2019-06-26

本文共 3229 字,大约阅读时间需要 10 分钟。

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代码:
1 #include 
2 using namespace std; 3 int main() 4 { 5 char a1[1005],b1[1005]; 6 int a[1005],b[1005],c[1005];//a,b,c分别存储加数,加数,结果 7 int x,i,j,n; 8 int lena,lenb,lenc; 9 while(scanf("%d",&n)!=EOF)10 {11 for(j=1;j<=n;j++)12 {13 memset(a,0,sizeof(a));//数组a清零14 memset(b,0,sizeof(b));//数组b清零15 memset(c,0,sizeof(c));//数组c清零16 scanf("%s%s",&a1,&b1);17 lena=strlen(a1);18 lenb=strlen(b1);19 for(i=0;i<=lena;i++)20 a[lena-i]=a1[i]-'0';//将数串a1转化为数组a,并倒序存储21 for(i=0;i<=lenb;i++)22 b[lenb-i]=b1[i]-'0';//将数串b1转化为数组a,并倒序存储23 x=0;//x是进位24 lenc=1;//lenc表示第几位25 while(lenc<=lena||lenc<=lenb)26 {27 c[lenc]=a[lenc]+b[lenc]+x;//第lenc位相加并加上次的进位28 x=c[lenc]/10;//向高位进位29 c[lenc]%=10;//存储第lenc位的值30 lenc++;//位置下标变量31 }32 c[lenc]=x;33 if(c[lenc]==0)//处理最高进位34 lenc--;35 printf("Case %d:\n",j);//格式要求吧!学着点36 printf("%s + %s = ",a1,b1);//这也是格式要求吧!学着点37 for(i=lenc;i>=1;i--)38 printf("%d",c[i]);39 printf("\n");40 if(j!=n)//对于2组之间加空行的情况41 printf("\n");42 }43 }44 return 0;45 }

 java写法大数,真是有毒!

1 import java.math.BigInteger; 2 import java.util.Scanner; 3  4 public class Main { 5  6     /** 7      * @param args 8      */ 9     public static void main(String[] args)10     {11         // TODO Auto-generated method stub12        //System.out.println("Hello World!");13        Scanner in=new Scanner(System.in);14        while(in.hasNextInt())15        {16 //           int []arr=new int[3];17            int  n;18            n=in.nextInt();19            for(int i=1;i<=n;i++)20            {21                BigInteger a,b;22                a=in.nextBigInteger();23                b=in.nextBigInteger();24                if(i

 

转载地址:http://sbtwl.baihongyu.com/

你可能感兴趣的文章
4.1Python文件基本操作
查看>>
nginx源码分析之线程池
查看>>
正向代理、透明代理、反向代理的理解示意图
查看>>
CSS清除浮动_清除float浮动
查看>>
Laravel User Agent 轻松识别客户端(微信)信息(2019版)
查看>>
嵌入式开发/调试辅助工具
查看>>
开发小技巧: 如何在jQuery中禁用或者启用滚动事件.scroll
查看>>
Todoist Chrome:待办事项列表及任务管理
查看>>
RxJava
查看>>
java日期多次使用修改,数据有问题
查看>>
强调编码标准
查看>>
聊聊springboot session timeout参数设置
查看>>
jvm内存对象分析
查看>>
聊聊rocketmq的SequenceProducerImpl
查看>>
HTML&CSS基础学习笔记8-预格式文本
查看>>
nginx + lua 构建网站防护waf(一)
查看>>
Django part 6 ---Static File
查看>>
安装office2007时发生错误1706解决方法
查看>>
Java实现生产者消费者问题与读者写者问题
查看>>
关于 epoch、 iteration和batchsize
查看>>