原题链接: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)

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

题目大意: 大数相加

题目思路: 从最低位开始一位一位的加。注意进位,注意清除前缀的0

C++ 代码如下:

#include <cstring>
#include <iostream> using namespace std; int main()
{
int n;
char a[1001],b[1001];
short c[1001];
cin >> n;
for (int i=0; i<n; i++)
{
int alen,blen,clen,maxlen;
cin >> a >> b;
alen = strlen(a);
blen = strlen(b);
clen = 0;
maxlen = alen/blen ? alen : blen;
int ai,bi,s=0;
for (int j=0; j<maxlen; j++)
{
ai = alen-j-1;
bi = blen-j-1;
if (ai>=0 && bi>=0)
s = a[ai]+b[bi]-2*'0'+s/10;
else if (ai>=0)
s = a[ai]-'0'+s/10;
else if (bi>=0)
s = b[bi]-'0'+s/10;
c[clen++] = s%10;
}
if (s/10)
c[clen++] = s/10;
for (int k=clen-1; k>=0; k--)
if (c[k])
break;
else
clen--;
cout << "Case " << i+1 << ":" << endl;
cout << a <<" + " << b << " = ";
for (int k=clen-1; k>=0; k--)
cout << c[k];
cout << endl;
if (n-i-1)
cout << endl;
}
return 0;
}

hduoj 1002 A + B Problem II的更多相关文章

  1. Problem : 1002 ( A + B Problem II )

    经验总结:一定要注意输出的格式,字符的空格,空行,一定要观察清楚.如本题的最后一个输出结果后面没有空行.最后代码实现的时候需要判断一下,代码如下 !=n) cout<<endl; Prob ...

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

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

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

  4. HDU 1002 A + B Problem II

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

  5. hdoj 1002 A + B Problem II

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

  6. hdoj 1002 A + B Problem II【大数加法】

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

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

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

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

  9. HPU 1002 A + B Problem II【大数】

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

随机推荐

  1. webpack应用案例之美团app

    记录自己的创建步骤,且对自己的错误进行纠正分析.

  2. C语言函数strstr

    函数原型: extern char *strstr(char *str1, const char *str2);   语法: * strstr(str1,str2)   参数: str1: 被查找目标 ...

  3. jquery 倒计时

    今天让我公司前端大神,李杨哥,给做了一个jquery倒计时功能  很牛逼 看下面的效果图 这个倒计时是需要传值的,看效果代码讲解  百度云盘 ,压缩包永久有效  链接: https://pan.bai ...

  4. 搭建Harbor企业级docker仓库

    搭建Harbor企业级docker仓库 一.Harbor简介 1.Harbor介绍 Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,通过添加一些企业必需的功能特性,例如 ...

  5. Web开发——JavaScript基础(JSON教程)

    参考: JSON:JavaScript 对象表示法(JavaScript Object Notation). JSON 是存储和交换文本信息的语法.类似 XML. JSON 比 XML 更小.更快,更 ...

  6. [yum] yum加速

    通常,以我对于个人生活和工作品质的要求,并没有这个需求. 因为我的宽带费很贵,独享.就算是centos主站,也很快.但是当你去了一些办公环境恶劣的地方上班的时候, 也难免动用一些小技巧.如下: 装这个 ...

  7. Struts2 学习

    Struts2简介 1.概念:轻量级的MVC框架,主要解决了请求分发的问题,重心在控制层和表现层.低侵入性,与业务代码的耦合度很低.Struts2实现了MVC,并提供了一系列API,采用模式化方式简化 ...

  8. Django进阶之查询优化、extra注入SQL及批量创建

    Django查询优化 Django的查询优化用到两个函数——select_related()和prefetch_related(). select_related()用的是连表join的方式,主要处理 ...

  9. 【JVM】-NO.113.JVM.1 -【JDK11 HashMap详解-0-全局-put】

    Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...

  10. python数据结构-如何根据字典中值的大小对字典项排序

    如何根据字典中值的大小对字典项排序 问题举例 某班英语成绩以字典形式存储,如何根据成绩高低,计算学生成绩排名 { “tom”:80, "lily":88, "marton ...