A + B Problem II 大数加法
题目描述:
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
代码如下:
#include<iostream>
#include<string>
using namespace std;
string ans;
void bintadd(string a,string b)
{
int alen = a.length() - ,blen = b.length() - ;//alen,blen分别存放a,b字符串的最大下标
int up = ,i = ,j,te;//up存放进位值,te存放余数 while(alen >= || blen >= )
{
if(alen >= && blen >= )//两个字符串都没有计算完
te = a[alen--] + b[blen--] + up -'' - '';//倒着计算字符串里的数,记得要加上进位值up
else
if(alen >= && blen < )//如果b字符串已经计算完
te = a[alen--] + up -'';
else
if(alen < && blen >= )//如果a字符串已经计算完
te = b[blen--] + up - '';
if(te > )
{
up = te / ;//得到进位值
te = te % ;//得到余数
}
else
up = ;
ans.push_back(te + '');//将结果放进结果字符串
}
if(up)
ans.push_back(up + '');//如果出现两个字符串一样长,而还有进位值
} int main()
{
string a,b;
int t,ca = ,i;
cin >> t;
for(ca = ;ca <= t;ca++)
{
cin >> a >> b;
bintadd(a,b);
if(ca != )
cout << endl;
cout << "Case" << " " << ca << ":" << endl;
cout << a << " + " << b << " = ";
for(i = ans.length() - ;i >= ;i--)//反向输出结果字符串
cout << ans[i];
cout << endl;
ans.erase();
}
}
代码分析:
上面的代码是我根据网上代码改正后的,我自己写的代码虽然答案正确,但没有上面的代码那么简洁,所以就用了网上的代码来分析,不过网上的这代码有错误,但竟然AC了。。。我改正后,也AC了。。
大数加法,主要就在于将数字存储为字符串,然后根据小学学的满十进一的原则,得出答案,最后反向输出。。。
参考地址:http://www.haogongju.net/art/1227474
A + B Problem II 大数加法的更多相关文章
- hdu1002 A + B Problem II[大数加法]
目录 题目地址 题干 代码和解释 参考 题目地址 hdu1002 题干 代码和解释 由题意这是一个涉及到大数的加法问题.去看了一眼大数加法的方法感觉头很大,然后突然发现Java可以流氓解决大数问题,毅 ...
- A + B Problem II(大数加法)
一直格式错误,不想改了,没A #include <iostream> #include <stdio.h> #include <string.h> #include ...
- hdu1002 A + B Problem II(大数题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java/ ...
- HDU1002 -A + B Problem II(大数a+b)
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过
杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...
- 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) ...
- hdu1002 A + B Problem II(高精度加法) 2016-05-19 12:00 106人阅读 评论(0) 收藏
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU1002 A + B Problem II 大数问题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java ...
- HDU 1023 Train Problem II 大数打表Catalan数
一个出栈有多少种顺序的问题.一般都知道是Catalan数了. 问题是这个Catalan数非常大,故此须要使用高精度计算. 并且打表会速度快非常多.打表公式要熟记: Catalan数公式 Cn=C(2n ...
随机推荐
- 破解win7开机密码,又不取消密码
Win7开机密码破解方法 1. 进入pe.(可用系统盘,或者u盘启动) 2.进入c:\windows\system32下 更改magnify.exe和cmd.exe的所有者为administrator ...
- POJ 3623 Best Cow Line, Gold(字符串处理)
题意:给你一个字符串,让你重新排列,只能从头或者尾部取出一个放到新字符串队列的最后.按照字典序. 解决方法:比较前后两个的大小,谁小输出谁,相等,就往当中比来确定当前应该拿最前面的还是最后面的,如果再 ...
- margin 属性的相关问题
1.margin 的IE6 双边距问题 问题描述:浮动的块挨边框的时候会产生双倍的边距 解决方案: 1.增加display:inline; 2.去除float属性 2.margin 的重叠问题 CSS ...
- Sliding Window Maximum
(http://leetcode.com/2011/01/sliding-window-maximum.html) A long array A[] is given to you. There is ...
- JavaScript基础知识----零基础js入门练习题
1,什么是Javascript? 答:Javascipt是一种脚本语言,由web浏览器进行解释和执行. 2,JavaScript是由那几个部分组成? 答:主要分为以下三种: ECMASc ...
- PHP冒泡排序法
冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法.法如其名,就是像冒泡一样,每次从数组当中 冒一个最大的数出来. 冒泡排序它重复地走访过要排序的数列,一次比较两个元素,如果他 ...
- ERROR 1044 (42000): Access denied for user 'root'@'localhost' to database 'information_schema'
在我想把备份的数据库导入到本地数据的时候,发生这个错误,我使用的工具是dbForge Studio for MySQL ERROR 1044 (42000): Access denied for us ...
- bzoj 1046 : [HAOI2007]上升序列 dp
题目链接 1046: [HAOI2007]上升序列 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3620 Solved: 1236[Submit] ...
- [LeetCode]题解(python):080-Remove Duplicates from Sorted Array II
题目来源: https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意分析: 跟定一个排好序的数组.修改这个数组使 ...
- 关于Python的self指向性
Python的self是指向类的实例化对像,而不是类本身,每次调用类的实例化即self指向此实例化对像,如下代码: class Person: def __init__(self,name): sel ...