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
代码如下:
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 string ans; 5 void bintadd(string a,string b) 6 { 7 int alen = a.length() - 1,blen = b.length() - 1;//alen,blen分别存放a,b字符串的最大下标 8 int up = 0,i = 0,j,te;//up存放进位值,te存放余数 9 10 while(alen >= 0 || blen >= 0) 11 { 12 if(alen >= 0 && blen >= 0)//两个字符串都没有计算完 13 te = a[alen--] + b[blen--] + up -'0' - '0';//倒着计算字符串里的数,记得要加上进位值up 14 else 15 if(alen >= 0 && blen < 0)//如果b字符串已经计算完 16 te = a[alen--] + up -'0'; 17 else 18 if(alen < 0 && blen >= 0)//如果a字符串已经计算完 19 te = b[blen--] + up - '0'; 20 if(te > 9) 21 { 22 up = te / 10;//得到进位值 23 te = te % 10;//得到余数 24 } 25 else 26 up = 0; 27 ans.push_back(te + '0');//将结果放进结果字符串 28 } 29 if(up) 30 ans.push_back(up + '0');//如果出现两个字符串一样长,而还有进位值 31 } 32 33 int main() 34 { 35 string a,b; 36 int t,ca = 0,i; 37 cin >> t; 38 for(ca = 1;ca <= t;ca++) 39 { 40 cin >> a >> b; 41 bintadd(a,b); 42 if(ca != 1) 43 cout << endl; 44 cout << "Case" << " " << ca << ":" << endl; 45 cout << a << " + " << b << " = "; 46 for(i = ans.length() - 1;i >= 0;i--)//反向输出结果字符串 47 cout << ans[i]; 48 cout << endl; 49 ans.erase(); 50 } 51 }
代码分析:
上面的代码是我根据网上代码改正后的,我自己写的代码虽然答案正确,但没有上面的代码那么简洁,所以就用了网上的代码来分析,不过网上的这代码有错误,但竟然AC了。。。我改正后,也AC了。。
大数加法,主要就在于将数字存储为字符串,然后根据小学学的满十进一的原则,得出答案,最后反向输出。。。
参考地址:http://www.haogongju.net/art/1227474
转载于:https://www.cnblogs.com/linxiaotao/p/3413384.html
A + B Problem II 大数加法相关推荐
- hdu A + B Problem II(大数相加,数组实现)
hdu A + B Problem II(大数相加,数组实现) 题目走起 注意最后一个case 不需要换行 下面代码 #include<stdio.h> #include<strin ...
- HDU1002 Problem II(大数相加)(C++题解)
A题 HDOJ1002(大数加法) A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/327 ...
- HDU - 1002 A + B Problem II(大数)
题目链接:点击查看 题目大意:大数加法 题目分析:第一次用Java写ACM,还是有点小激动滴,有几点需要注意的: 公共类名要写Main 不要提交包名 注意输入输出 上代码吧,纪念一下: import ...
- java棋盘问题_0x03大数问题(JAVA解决棋盘覆盖,A+B Problem II)
常见的有棋盘覆盖和A+B问题,这类问题牵扯到的数值都比较大,如果用一般的数值类型,肯定输出不了,所以就要想一个办法,怎么把大数转换一下输出. A+B Problem II 时间限制:3000 ms | ...
- HPU 1002 A + B Problem II【大数】
HPU 1002 A + B Problem II[大数] A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- LeetCode 445. 两数相加 II(链表大数加法)
1. 题目 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 进 ...
- Hdu - 1002 - A + B Problem II
题目: A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- HDU 1002 A + B Problem II
ACM OJ题解目录 本题网址:https://cn.vjudge.net/problem/HDU-1002 A + B Problem II I have a very simple problem ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
最新文章
- 为什么要看源码、如何看源码,高手进阶必看
- 使用DBCA工具创建自己的数据库
- LNCS用户写作指南【 Springer Computer Science Proceedings 】
- css-背景图片和渐变
- Bailian3718 位操作练习【位运算】
- 压力测试与负载测试的区别
- mybatis mapper配置 bigint_Mybatis-plus 入门
- 红黑所-1996-2011年中国黑客大事记
- 计算机软硬件逻辑等价性是指,南航计算机组成原理复习ppt.ppt
- cnpm 安装文件找不到_技术员修复 win7系统word2013找不到标尺工具的处理办法 -win7系统使用教程...
- Origin——在对象管理器中操作图层和绘图
- 断网重启路由器就好_为什么总是断网重启路由器就好了
- Tri-BACKUP Pro 9 Mac磁盘数据备份软件
- STM32F407VET6读写GD25Q16(W25Q16)
- js逆向加密五邑大学教务系统密码AES实现模拟登录(仅供参考)
- 苹果电脑查看已经连上的WiFi密码(亲测可用)
- 【ArangoDB 介绍】
- c语言编程单片机中的sbit,单片机sfr和sbit的用法
- C语言复习03(小甲鱼版本)
- 小白教程:快速在IDEA上创建包和类(java)
热门文章
- html图标框架中文版,常见css框架有哪些?
- 梯度下降python编程实现_【机器学习】线性回归——单变量梯度下降的实现(Python版)...
- openlayers添加按钮_OpenLayers3加载常用控件使用方法详解
- charles 代理手机连不上网_「技巧」不想接电话?这样可以让手机变成空号,还不影响上网...
- 【clickhouse】ClickHouseException code: 225 ZooKeeper session has been expired
- Netty实战项目:Spring boot 程序的聊天程序
- 【FLink】Flink checkpoint 实现数据连续计算 恢复机制 案例实战
- 【java】我工作三年了,该懂并发了!
- Kafka : FileNotFoundException索引文件丢失 xxx.index (No such file or directory)
- Spring : ImportBeanDefinitionRegistrar源码分析