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 ...
随机推荐
- PROS Step:只需几分钟即可创建优化的价目表,并发现即时收益机会。
多年来,各个公司一直使用手动流程和电子表格来制定产品和服务定价,而没有真正意义上的方法或策略.在我写这篇文章时仍然如此! 但是,如今的形势已经改变.利用 PROS Step,公司可以将其数据上传到 M ...
- java字符串函数及理解
Java中的字符串也是一连串的字符.但是与许多其他的计算机语言将字符串作为字符数组处理不同,Java将字符串作为String类型对象来处理.将字符串作为内置的对象处理允许Java提供十分丰富的功能特性 ...
- android错误之==与equals的区别
在做一个电话号码匹配的时候, 开始我使用的是这种情况 if (num == inCall.getNum()) { //操作... } 但是无论如何结果都是false,就算两个号码打印出来一模一样还是f ...
- matlab画甘特图
近期为发小论文一直在研究作业调度问题,好不easy把数据搞出来了,结果又被画甘特图给难住了,查了各种资料.anygantt,highchart.Jfree chart等都试了,效果都不咋好.无意中留意 ...
- SharePoint 2007 (MOSS/WSS) - how to remove "Download a Copy" context menu from a Document Library
One of my friend and colleague asked me this question. I found it tricky and a good post for my blog ...
- Java中布尔类型操作符&=,|=与^=的使用
今天在对同事的代码进行code review的时候,见到一个比较好玩的写法.“flag &= false:”,乍一看,还感觉他写错了,但是程序可以正常运行,赶紧去百度,看一下这个写法到底是怎么 ...
- Javaweb统计在线人数的小栗子
最近在学习Javaweb相关的内容(不黑不吹之前对web开发零基础),下面通过一个统计在线人数的小栗子讲讲Servlet监听器吧 开发环境 eclipse tomcat 7 先说说这个小栗子的构思: ...
- hdu 4349 Xiao Ming's Hope lucas
题目链接 给一个n, 求C(n, 0), C(n, 1), ..........C(n, n)里面有多少个是奇数. 我们考虑lucas定理, C(n, m) %2= C(n%2, m%2)*C(n/2 ...
- 【源码】实现Android闹钟功能使用HTML+JS,并附带Alarm代码分享
先是开发手册中最终功能的实现函数,再是Alarm接口的源码. 参数: argu:型如“key1=value1;key2=value2;......”的参数表.首先,该参数表支持 rexseeNotif ...
- 介绍一个python的新的web framework——karloop框架
karloop是一款轻型的web framework,和tornado.webpy类似.mvc分层设计,眼下已经公布早期版本号了,使用方便, 下载地址例如以下:https://github.com/k ...