原题链接: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. 1. node.js 认识 (一)

    官网: (下载安装nodejs) https://nodejs.org/en/ http://nodejs.cn/ Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环 ...

  2. 【Swing程序设计/常用面板】

    面板也是一个Swing容器,可以作为容器容纳其他组件,但是它必须要被添加到其他容器中.Swing常见的面板是JPanel面板和JScrollPanel面板. JPanel面板可以聚集一些组件来布局. ...

  3. vue里的v-show和v-if

    v-show:false            对应的是display:none:不移除dom元素.对网页渲染性能更好,适应于频繁的操作该dom的显示隐藏. v-if:  false       对应 ...

  4. kubernetes组成

    kubernetes组成 k8s主要包括: kubectl 客户端命令行工具: 将接收的命令,发送给kube-apiserver,作为对整个平台操作的入口. kube-apiserver REST A ...

  5. 利用XML语法 SQL 列转行

    --行转列 固定xml语法 declare @xml xml ; set @xml=cast('<v>2</v><v>4</v><v>3&l ...

  6. RFID系统 免费开源代码 开发,分享[申明:来源于网络]

    RFID系统 免费开源代码 开发,分享[申明:来源于网络] 地址:http://www.codeforge.cn/s/0/RFID%E7%B3%BB%E7%BB%9F

  7. Python idle运行代码出现'ascii' codec can't encode characters in position 0-2

    编码问题,采用一种方法: Python代码 ,开头加: import sys reload(sys) sys.setdefaultencoding('utf8') 在idle中运行后没错误,但是不显示 ...

  8. java之动态代理

    摘要 相比于静态代理,动态代理避免了开发人员编写各个繁锁的静态代理类,只需简单地指定一组接口及目标类对象就能动态的获得代理对象. 这里说的静态代理可以理解为之前使用的装饰者模式,从之前使用装饰者模式实 ...

  9. abap 断言

    1: Assert equal http://www.saptechnical.com/Tutorials/OOPS/ABAPUnit/Index.htm

  10. Java 基础 IO流(转换流,缓冲)

    一,前言 在学习字符流(FileReader.FileWriter)的时候,其中说如果需要指定编码和缓冲区大小时,可以在字节流的基础上,构造一个InputStreamReader或者OutputStr ...