原题链接: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. 24.redux

    Flux:Flux 是一种架构思想 https://facebook.github.io/flux/ 官网 资料: http://www.ruanyifeng.com/blog/2016/01/flu ...

  2. CF1B Spreadsheets

    题意翻译 人们常用的电子表格软件(比如: Excel)采用如下所述的坐标系统: 第一列被标为A,第二列为B,以此类推,第26列为Z.接下来为由两个字母构成的列号: 第27列为AA,第28列为AB... ...

  3. yarn集群客户端gateway常用限制

    spark默认集群模式,省略上传依赖包过程:spark-default.sh spark.yarn.jars hdfs:///${PATH}/sparkJar/jars/*.jarspark.subm ...

  4. 文本编辑器vim和gedit

    vim和gedit都是文本编辑器 vim用法: vim 文件名 #打开文件 输入 i,进入文本编辑模式,编辑完再按ESC,退出编辑模式,再输:wq,保存退出:如果输:q!则是不保存退出,很简单.. 如 ...

  5. python语法_input

    input:与用户的交互,返回用户输入的值 注意:input接受的所有数据都为字符串,即便输入的为数字,依然会被当成字符串

  6. day17 十七、时间模块

    一.时间模块 import time print(time) # <module 'time' (built-in)> import time print('暂停开始') secs = t ...

  7. Codeforces 1136E - Nastya Hasn't Written a Legend - [线段树+二分]

    题目链接:https://codeforces.com/problemset/problem/1136/E 题意: 给出一个 $a[1 \sim n]$,以及一个 $k[1 \sim (n-1)]$, ...

  8. H5的缓存 manifest

    H5里面的App Cache是由开发Web页面的开发者控制的,而不是由Native去控制的,但是Native里面的WebView也需要我们做一下设置才能支持H5的这个特性. 1.工作原理 写Web页面 ...

  9. [efficiency] emacs入门

    一. 没记错的话,这可能是第三次读emacs tutorial了.前两次读的非常慢,也不记得有没有读完了.总之最后都忘光了. 这次读的很顺畅,利用工作的空闲时间加上今天晚上(周日).总算是读完了. 没 ...

  10. [tldk][dpdk][dev] TLDK--基于dpdk的用户态协议栈传输层组件简单调研

    如题,以下是一份简单的快速调研. TLDK: Transport Layer Development Kit 一 什么是TLDK transport layer development kit 处理t ...