hduoj 1002 A + B Problem II
原题链接: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的更多相关文章
- Problem : 1002 ( A + B Problem II )
经验总结:一定要注意输出的格式,字符的空格,空行,一定要观察清楚.如本题的最后一个输出结果后面没有空行.最后代码实现的时候需要判断一下,代码如下 !=n) cout<<endl; Prob ...
- 抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)
数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. while (a) //将每位数字取出来, ...
- 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) ...
- HDU 1002 A + B Problem II
A + B Problem II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted ...
- hdoj 1002 A + B Problem II
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdoj 1002 A + B Problem II【大数加法】
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1002 A + B Problem II(大整数相加)
A + B Problem II Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...
- 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) ...
- HPU 1002 A + B Problem II【大数】
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- json转换学习
文章部分代码实体类可以忽略. 原文:https://www.cnblogs.com/free-dom/p/5801866.html 代码如下: package covert; import java. ...
- PHP 标准规范,PSR-1,PSR-2,PSR-3,PSR-4,PSR-5,PSR-6,PSR-7及其他标准
官方网站:https://psr.phphub.org/ 这里还有其他很多规范,但是很多都是英文. github:https://github.com/summerblue/psr.phphub.or ...
- spark streaming集成kafka
Kakfa起初是由LinkedIn公司开发的一个分布式的消息系统,后成为Apache的一部分,它使用Scala编写,以可水平扩展和高吞吐率而被广泛使用.目前越来越多的开源分布式处理系统如Clouder ...
- Spring Boot 入门day01
一.Spring Boot入门 1.Spring Boot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特 ...
- zabbix3.2使用自带模板监控MySql
一.zabbix自带MySql模板监控项 Zabbix3.0之后已经有MySql监控模板了,所以我们只要引用ZabbixServer自带的模板即可.zabbix默认有14个监控项 我们只需获取监控项需 ...
- ionic 需要注意的知识点
- 15.IEnumerable和IEnumerator
先说IEnumerable,我们每天用的foreach你真的懂它吗? 阅读目录 自己实现迭代器 yield的使用 怎样高性能的随机取IEnumerable中的值 我们先思考几个问题: 为什么在fore ...
- Qt setMouseTracking(true) 无效
网友1:并非只在QWidget中设置setMouseTracking(true)才好用,如若在QMainwindow中设置为true还是不能跟踪,解决办法为在ui中的属性栏主窗口的“mouseTrac ...
- C#ImageList和ListView的使用
一.ImageList ImageList组件,又称为图片存储组件,它主要用于存储图片资源,然后在控件上显示出来,这样就简化了对图片的管理.ImageList组件的主要属性是Images,它包含关联 ...
- scrum学习笔记
http://www.scrumcn.com/agile/scrum-knowledge-library/scrum.html#tab-id-1 推荐电子书 <Scrum精髓_敏捷转型指南> ...