题目:
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. 

InputThe 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. 
OutputFor 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
解题思路:
既然是大整数,肯定就不能用实型来储存变量,在这里使用字符串string类型来进行模拟运算。(即:从个位数加起,大于10就进1,以此类推。)
有两个进位的地方需要注意!一个是当短的那个数加完时,另一个是长的那个数加完时(比如:1+9999和543+457特别注意)
下面给出大整数加法函数:
 string add(string a,string b)
{
string c=""; //长的那个数进位时直接在结果前面加个‘1’。
int cur=,d=,e=;
if (a.size()<b.size())swap(a,b); //a为长的数,b为短的。swap(a,b)交换a,b。
int la=a.size();
int lb=b.size();
while (lb--) //把短的那个数计算完。
{
la--;
d=(cur+a[la]-''+b[lb]-'')/; //判断是否有进位。
a[la]=(cur+a[la]-''+b[lb]-'')%+'';
cur=d;
}
if (cur==) //如果短的那个数最后一位数还有进位。
while (la--)
{
e=(a[la]-''+cur)/;
a[la]=(a[la]-''+cur)%+'';
cur=e;
}
if (cur==) //如果长的那个数最后一位还有进位。
a=c+a;
return a;
}

A——大整数加法(HDU1002)的更多相关文章

  1. 大整数加法 HDU1002

    今天早上没事干又把这个敲了一遍,虽然手冻得不行,不过又深入理解理解还可以哈. 难点就在给你的整数可能很大很长,所以long long 肯定不行,得用字符串来读取存储,然后注意一下相加的时候进位,最后输 ...

  2. HDU1002——大整数加法

    题目: I have a very simple problem for you. Given two integers A and B, your job is to calculate the S ...

  3. AC日记——大整数加法 openjudge 1.6 10

    10:大整数加法 总时间限制:  1000ms 内存限制:  65536kB 描述 求两个不超过200位的非负整数的和. 输入 有两行,每行是一个不超过200位的非负整数,可能有多余的前导0. 输出 ...

  4. 2981:大整数加法-poj

    2981:大整数加法 总时间限制:  1000ms 内存限制:  65536kB 描述 求两个不超过200位的非负整数的和. 输入 有两行,每行是一个不超过200位的非负整数,可能有多余的前导0. 输 ...

  5. POJ 2506 Tiling(递推+大整数加法)

    http://poj.org/problem?id=2506 题意: 思路:递推.a[i]=a[i-1]+2*a[i-2]. 计算的时候是大整数加法.错了好久,忘记考虑1了...晕倒. #includ ...

  6. openjudge计算概论-大整数加法

    /*=====================================================================1004:大整数加法总时间限制: 1000ms 内存限制: ...

  7. 剑指offer第12题打印从1到n位数以及大整数加法乘法

       字符和数字加减就是字符的ASCII码和数字直接加减. 方法一: 1)在字符串操作中给一个整形数字加(字符0)就是把它转化为字符,当然给一个字符减去(字符0)就可以把它转化为数字了:如果确实是最后 ...

  8. Javascript实现大整数加法

    记得之前面试还被问到过用两个字符串实现两个大整数相加,当时还特别好奇好好的整数相加,为什么要用字符串去执行.哈哈,感觉当时自己还是很无知的,面试官肯定特别的无奈.今天在刷算法的时候,无意中看到了为什么 ...

  9. [CodeWars][JS]实现大整数加法

    问题描述 实现‘字符串加法’,即将两个以字符串形式表示的数字相加,得到结果然后返回一个新的字符串. 例如:输入‘123’,‘321’,返回‘444’. 这样在进行两个任意大的整数相加的时候,既不会溢出 ...

随机推荐

  1. AFN请求问题

    在使用AFNetworking 2.0  的时候本来一切很顺畅,但是中途遇到几个比较坑的地方 在发送请求后,NSURLSessionDataTask一直报错 Error Domain=com.alam ...

  2. python 栈

    栈的特点:先进后出 class Stack: def __init__(self): self.data = [] def push(self, val): self.data.append(val) ...

  3. mysql 行增删改查

    一.增 ); ),(); insert into student(name, age) select name, age from info; 二.删 delete from db1; delete ...

  4. 异数OS-织梦师-异数OS虚拟容器交换机(七) 走进4Tbps网络应用时代,加速5G应用真正落地

    . 异数OS-织梦师-异数OS虚拟容器交换机(七) 走进4Tbps网络应用时代,加速5G应用真正落地 本文来自异数OS社区 github: https://github.com/yds086/Here ...

  5. maven-assembly-plugin入门

    愿文地址:https://www.jianshu.com/p/e8585a991e8e 当你使用 Maven 对项目打包时,你需要了解以下 3 个打包 plugin,它们分别是 plugin func ...

  6. mybatis in查询

    原文地址:https://blog.csdn.net/u012427355/article/details/79580561 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集 ...

  7. fastjson中Map与JSONObject互换

    1.//将map转换成jsonObject JSONObject itemJSONObj = JSONObject.parseObject(JSON.toJSONString(itemMap)); 将 ...

  8. 利用Python进行博客图片压缩

    自己写博客的时候常常要插入一些手机拍的照片,都是几M的大小,每张手动压缩太费事了,于是根据自己博客的排版特点用Python写了一个简单的图片压缩脚本,功能是将博客图片生成缩略图,横屏的图片压缩为宽度最 ...

  9. 前端 network

    控制台 :https://blog.csdn.net/m0_37724356/article/details/79884006 原文链接:https://segmentfault.com/a/1190 ...

  10. Windows PHP 开启opcache的方法

    PHP opcache可以提升性能.Windows PHP 配置 opcache 的方法如下: 1.先检查PHP目录下ext目录中有没有php_opcache.dll,没有的话自己下载(PHP 5.5 ...