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. ubuntu下boost编译安装

    ubuntu下boost编译安装 boost 安装 1.依赖安装 apt-get install mpi-default-dev libicu-dev python-dev python3-dev l ...

  2. rxjs-流式编程

    前言 第一次接触rxjs也是因为angular2应用,内置了rxjs的依赖,了解之后发现它的强大,是一个可以代替promise的框架,但是只处理promise的东西有点拿尚方宝剑砍蚊子的意思. 如果我 ...

  3. nginx笔记2-负载均衡

    负载均衡实现方式分为两类:1硬件类,2软件类. 硬件类:F5(这是一种硬件,并不是刷新啊,不要误解)  优点:非常快,可靠性高,并发量大.缺点:太贵,成本高,不方便,最致命的是不能将动态请求和静态请求 ...

  4. 借鉴mini2440的usb-wifi工具集在Beagleboard上移植无线网卡

    配置minicom: sudo yum install minicom sudo minicom -s 选择Serial port setup,此时所示光标在"Change which se ...

  5. linux Cacti监控服务器搭建

    搭建Cacti监控服务器 部署安装环境(lamp) [root@zhuji1 ~]# yum -y install httpd [root@zhuji1 ~]# yum -y install php ...

  6. MFC: 获得关机消息;阻止Windows关机

    WM_QUERYENDSESSION消息是Windows向你询问Windows能否关闭,WM_ENDSESSION消息表示提示你Windows即将关闭.故当应用程序退出时, WM_QUERYENDSE ...

  7. Android可以拖动位置的ListVeiw

    参考网址: 1.https://github.com/bauerca/drag-sort-listview 2.http://www.tuicool.com/articles/jyA3MrU

  8. R语言之内存管理

    转载于:http://blog.csdn.net/hubifeng/article/details/41113789 在处理大型数据过程中,R语言的内存管理就显得十分重要,以下介绍几种常用的处理方法. ...

  9. 【原】eclipse创建maven工程时,如何修改默认JDK版本?

    问题描述:eclipse建立maven项目时,JDK版本默认是1.5,想创建时默认版本设置为1.8,如何修改? 解决方案: 找到本机maven仓库存放位置,比如:${user.home}/.m2/路径 ...

  10. Drying POJ - 3104

    It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She ...