题目链接

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

分析:

大数问题肯定是当作字符串来处理的,首先要把数的每个位数逆序存到一个int型的数组中,然后将这两个数组的对应位置上的数相加,如果有进位的话还应加上进位,最后输出的时候应该先找到首个不为0 的数,然后将后面的数输出来。

代码:

#include<iostream>
#include<math.h>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
getchar();
for(int h=1; h<=n; h++)
{
char a[10000],b[10000];
int c[10000],d[10000];
int e[10000];
int k,l;
memset(c,0,sizeof(c));
memset(d,0,sizeof(d));
scanf(" %s %s",a,b);
k=strlen(a);
l=strlen(b);
for(int i=0; i<k; i++)
c[i]=a[k-i-1]-'0';
for(int i=0; i<l; i++)
d[i]=b[l-i-1]-'0';
int op=0;
for(int i=0; i<1000; i++)
{
e[i]=(c[i])+(d[i])+op;
if(e[i]>=10)
{
e[i]=e[i]%10;
op=1;
}
else op=0;
}
printf("Case %d:\n",h);
printf("%s + %s = ",a,b);
int i;
for( i=999; i>=0; i--)
if(e[i]!=0)break;
for(int j=i; j>=0; j--)
printf("%d",e[j]);
printf("\n");
if(h!=n)
printf("\n");
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
}
return 0;
}

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

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

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

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

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

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

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

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

  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. A + B Problem II 大数加法

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

  8. hdu 1002 A + B Problem II(大数)

    题意:就是求a+b (a,b都不超过1000位) 思路:用数组存储 第一道大数的题目,虽然很水,纪念一下! 代码: #include<cstdio> #include<cstring ...

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

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

随机推荐

  1. 某一线互联网公司前端面试题总结css部分

    1,css3选择器 :not(selector) 选择页面内所有type!=text的类型: input:not([type=text]){ color: red; font-weight: bold ...

  2. Java接口成员变量

    定义接口    使用interface来定义一个接口.接口定义同类的定义类似,也是分为接口的声明和接口体,当中接口体由常量定义和方法定义两部分组成.定义接口的基本格式例如以下: [修饰符] inter ...

  3. [WC2005]友好的生物

    description 洛谷 求 \[max_{1\le i<j\le n}\{\sum_{s=1}^{k-1}(C_s-|D_{is}-D_{js}|)-(C_k-|D_{ik}-D_{jk} ...

  4. Android中WebView的跨域漏洞分析和应用被克隆问题情景还原(免Root获取应用沙盒数据)

    一.前言 去年年底支付宝的被克隆漏洞被爆出,无独有偶就是腾讯干的,其实真正了解这个事件之后会发现,感觉是针对支付宝.因为这个漏洞找出肯定花费了很大劲,主要是因为支付宝的特殊业务需要开启了WebView ...

  5. 洛谷4525 & 4526:【模板】自适应辛普森法——题解

    参考:https://phqghume.github.io/2018/05/19/%E8%87%AA%E9%80%82%E5%BA%94%E8%BE%9B%E6%99%AE%E6%A3%AE%E6%B ...

  6. LOJ2351:[JOI2017/2018决赛]毒蛇越狱——题解

    https://loj.ac/problem/2351 参考:https://www.cnblogs.com/ivorysi/p/9144676.html 但是参考博客讲解太吓人了,我们换一种通俗易懂 ...

  7. [bzoj] 1040 骑士 || 基环外向树dp

    原题 给出n个点n条边和每个点的点权,一条边的两个断点不能同时选择,问最大可以选多少. //图是一张基环外向树森林 是不是很像舞会啊- 就是多了一条边. 所以我们考虑一下对于一棵基环外向树,拆掉一条在 ...

  8. [转载]php中sleep,flush,ob_flush函数介绍

    <?phpecho str_pad(" ",1024);//当上面这句没有的时候浏览器没有任何输出 直到sleep函数设定的时间结束 才会输出//原因如下面截图for ($i ...

  9. Nginx漏洞利用与安全加固

    本文主要分为两大部分,第一部分介绍了Nginx的一些常见安全漏洞的形成原因.利用方法,并给出了相应的解决办法;第二部分介绍了Nginx安全加固时需要关注的主要内容. Nginx(发音同engine x ...

  10. 如何使用Navicat连接Oracle

    1.Navicat连接Oracle,需要使用OCI库.因此先要安装Oracle提供的客户端instantclient-basic, 请注意,32位的Navicat需要下载配置32位的instantcl ...