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

问题分析

此题是有一定难度的,其难点在于,如何表示当有1000位数的数字,因为就算是long long 型也是无法表达这么大的数字的(long long 是能表示出最大的数字的数据类型,其范围为9223372036854775807 ~ -9223372036854775808),因此应考虑采用数组进行存储,把我们输入的数转换成字符串,再将其转换为整数,另外,在本题中要注意相加的几种情况,

第一种,A.length==B.length

第二种,A.length>B.length

第三种,A.length<B.length

对于每种情况应该如何考虑,最后要注意题中所给的输入、输出格式。

Code:

#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
string a,b;
int sum[],q;//q为相加之后的位数
void add2(string a,string b) {
/*两个大数相加求和*/
int m,n,t=,i=,int_a,int_b;
//t为进位数
m=a.length()-;
n=b.length()-;
while(m>=&&n>=){
int_a=a[m]-'';//将输入的每一位数先转换成int型
int_b=b[n]-'';
sum[i]=(int_a+int_b+t)%;//相加后第i位的数字
t=(int_a+int_b+t)/;//相加后向上一位进的位数
i++;
m--;
n--;
}
if(m>n){
while(m>=){
int_a=a[m]-'';
sum[i]=(int_a+t)%;
t=(int_a+t)/;
m--;
i++;
} }
if(m<n){
while(n>=){
int_b=b[n]-'';
sum[i]=(int_b+t)%;
t=(int_b+t)/;
n--;
i++;
} }
q=i-;
if(t>=)//最后一次相加存在进位时
{
sum[i]=t;
q=i;
} }
void output() {
/*输出函数*/
int i=q;
cout<<a<<" + "<<b<<" = ";
for(;i>=;i--){ cout<<sum[i];
}
cout<<endl; }
int main() {
int n;//测试用例数
cin>>n;
int i=;
for(; i<n; i++) {
cin>>a>>b;
add2(a,b);
printf("Case %d:\n",i+);//测试样例格式
output();
if(i<n-)
cout<<endl;
}
return ;
}
 

杭电oj-1002-A+B Problem的更多相关文章

  1. 杭电oj 1016 Prime Ring Problem

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. 杭电oj 1002

    #include <iostream> #include <algorithm> using namespace std; int nCases; ], n[]; ], b[] ...

  3. 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)

    今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...

  4. C#利用POST实现杭电oj的AC自动机器人,AC率高达50%~~

    暑假集训虽然很快乐,偶尔也会比较枯燥,,这个时候就需要自娱自乐... 然后看hdu的排行榜发现,除了一些是虚拟测评机的账号以外,有几个都是AC自动机器人 然后发现有一位作者是用网页填表然后按钮模拟,, ...

  5. 杭电oj 2095 & 异或^符号在C/C++中的使用

    异或^符号,在平时的学习时可能遇到的不多,不过有时使用得当可以发挥意想不到的结果. 值得注意的是,异或运算是建立在二进制基础上的,所有运算过程都是按位异或(即相同为0,不同为1,也称模二加),得到最终 ...

  6. 用python爬取杭电oj的数据

    暑假集训主要是在杭电oj上面刷题,白天与算法作斗争,晚上望干点自己喜欢的事情! 首先,确定要爬取哪些数据: 如上图所示,题目ID,名称,accepted,submissions,都很有用. 查看源代码 ...

  7. 杭电oj 4004---The Frog Games java解法

    import java.util.Arrays; import java.util.Scanner; //杭电oj 4004 //解题思路:利用二分法查找,即先选取跳跃距离的区间,从最大到最小, // ...

  8. 杭电oj————2057(java)

    question:A+ B again 思路:额,没啥思路/捂脸,用java的long包里的方法,很简单,只是有几次WA,有几点要注意一下 注意:如果数字有加号要删除掉,这里用到了正则表达式“\\+” ...

  9. 爬取杭电oj所有题目

    杭电oj并没有反爬 所以直接爬就好了 直接贴源码(参数可改,循环次数可改,存储路径可改) import requests from bs4 import BeautifulSoup import ti ...

  10. 杭电acm 1002 大数模板(一)

    从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...

随机推荐

  1. bzoj 1171 大sz的游戏& 2892 强袭作战 (线段树+单调队列+永久性flag)

    大sz的游戏 Time Limit: 50 Sec  Memory Limit: 357 MBSubmit: 536  Solved: 143[Submit][Status][Discuss] Des ...

  2. 关键字final的用法

    final关键字可以用来修饰类.方法和变量. 1.final修饰的类不能被继承. 2.final修饰的方法不能被重写. 3.final修饰的变量是常量,不能修改其值.

  3. R语言学习笔记︱Echarts与R的可视化包——地区地图

    笔者寄语:感谢CDA DSC训练营周末上完课,常老师.曾柯老师加了小课,讲了echart与R结合的函数包recharts的一些基本用法.通过对比谢益辉老师GitHub的说明文档,曾柯老师极大地简化了一 ...

  4. java注解之二

    从JDK5开始,Java增加了Annotation(注解),Annotation是代码里的特殊标记,这些标记可以在编译.类加载.运行时被读取,并执行相应的处理.通过使用Annotation,开发人员可 ...

  5. dedecms 在首页调取文章内容

    方法一:arcticle 标签 加上channeleid {dede:arclist' addfields='body' channelid='1'}[field:body/]{/dede:arcli ...

  6. 错误代码: 1327 Undeclared variable: p_film_count

    1.错误描述 1 queries executed, 0 success, 1 errors, 0 warnings 查询:SELECT FOUND_ROWS() INTO p_film_count ...

  7. MySQL插入数据异常

    MySQL插入数据异常 1.错误如下: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:Dupli ...

  8. 错误代码: 1381 You are not using binary logging

    1.错误描述 1 queries executed, 0 success, 1 errors, 0 warnings 查询:show master logs 错误代码: 1381 You are no ...

  9. Ubuntu 上 hi3531 交叉编译环境 arm-hisiv100nptl-linux 搭建过程

    安装SDK 1.Hi3531 SDK包位置     在"Hi3531_V100R001***/01.software/board"目录下,您可以看到一个 Hi3531_SDK_Vx ...

  10. ubuntu14.04 编译安装highpoint rocketraid 2720驱动

    highpoint官方只有ubuntu12.10驱动,对于ubuntu14.04或者12.04,需要手工编译安装驱动. 基本步骤: 1.下载rocketraid 2720驱动源代码 2.在一台装有ub ...