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 ...
随机推荐
- epoll使用详解(精髓)
epoll使用详解(精髓) epoll - I/O event notification facility 在linux的网络编程中,很长的时间都在使用select来做事件触发.在linux新的内核中 ...
- SQL修炼道路上必看的书籍
1 SQL应用重构 9787111263586 2 SQL 必知必会(第3版) 9787115162601 3 SQL Server 2005高级程序设计 9787115170798 4 SQL 解惑 ...
- ASP.NET之电子商务系统开发-1(数据列表)
一.前言 首先声明的是,这是我第一个与别人合作的.net项目,另一个人做的是后台管理,我做的前台,这是一个电子商务的系统,主要实现的功能是查看商品以及购物功能. 二.开始 首先看一下我截取的项目部分商 ...
- [Swust OJ 582]--放学了,抢机子了(SPFA)
题目链接:http://acm.swust.edu.cn/problem/0582/ Time limit(ms): 5000 Memory limit(kb): 65535 Descriptio ...
- 建造者模式->代码示例
<?php interface Builder{ public function head(); public function body(); public function foot(); ...
- easyui form提交文件(上传图片和文件)
<div id="dialogBtn"> <a class="easyui-linkbutton" href="#" on ...
- Linux 网络编程: echo Service
前言 大病初愈,感谢某人的陪伴,感谢王乐庆同学和赵攀同学的细心照顾.原以为过了第八周就不忙了,却没想到还有明天的党章考试.还是写代码比背党章有意思~趁着服务器还没过期,赶紧把 echo 完成了.关于错 ...
- PHP学习笔记12-上传文件
上传图片文件并在页面上显示出图片 enctype介绍:enctype属性指定将数据发回到服务器时浏览器使用的编码类型. 取值说明: multipart/form-data: 窗体数据被编码为一条消息, ...
- ie7下div覆盖在iframe上方,ie8就不行,怎么解决
<div style="position:relative;display:inline-block;width:178px;height:90px;z-index:9999;top: ...
- 模拟美萍加密狗--Rockey2虚拟狗(四)
目录(?)[+] 首先,抱怨一下.学校个破网,似乎把我端口封了,死活分不上IP,也许是是我MAC改的太频繁了,有盗号嫌疑…… 然后,正文开始…… 其实虚拟狗几天前就写完了,可这几天上不了网 ...