博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
大数 a+b
阅读量:5741 次
发布时间:2019-06-18

本文共 3051 字,大约阅读时间需要 10 分钟。

Problem Description

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
 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
 
View Code
1 #include
2 #include
3 using namespace std; 4 5 #define P(x) \ 6 cout << #x " " << x << ":" << endl; 7 8 #define PE(a, b, c, sum) \ 9 cout << a << " + " << b << " = " << (c ? "1" : "") << sum << endl;10 int main()11 {12 int T;13 cin >> T;14 int Case = 1;15 while(T--)16 {17 P(Case);18 ++Case;19 string a, b, sum;20 cin >> a >> b;21 //cout << a << " + " << b << " = " ;22 int l_a = a.size()-1, l_b = b.size()-1, c = 0;23 sum = (l_a > l_b ? a : b);24 int i, j;25 for(i = l_a, j = l_b; i >= 0 && j >= 0; --i, --j)26 {27 int s = a[i] - '0' + b[j] - '0' + c;28 sum[i>j?i:j] = s % 10 + 48;29 c = s / 10;30 }31 while(i >= 0)32 {
//处理进位33 int s = sum[i] - '0' + c;34 sum[i] = s % 10 + 48;35 c = s / 10;36 i--;37 }38 while(j >= 0)39 {
//处理 进位40 int s = sum[j] - '0' + c;41 sum[j] = s % 10 + 48;42 c = s / 10;43 j--;44 }45 // cout << (c ? "1" : "") << sum << endl;46 PE(a, b, c, sum);47 if(T)48 cout << endl;49 }50 return 0;51 }

 

问题探究
#include
#include
using namespace std;const int maxn = 500;#define P(x) \cout << #x ": " << x << endl;int main(){ string a; cin >> a; //string str; //string str("shi"); string str(maxn, 'a'); //str = ""; //长度也变为了0 for(int i = 0; i != a.size(); ++i) //the same above //for(int i = a.size()-1; i >= 0; --i) str[i] = a[i]; P(a); P(str); return 0;}/*str: ""output: ""str:"shi"output: san由此可见,string 对象是有默认长度的(根据其赋值对象的长度决定)与字符数组类似的,只不过不需明确指明,可根据赋值对象动态的适应并改变它。字符赋值,其实质是在原有的基础上修改,是有长度限制的。但是后面以后str = "";以后长度也都变为了0.由此可见,使用字符赋值的关键是如何确定他的可能长度。*/

 

posted on
2013-02-22 10:10 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/sanghai/archive/2013/02/22/2921801.html

你可能感兴趣的文章
JavaScript 操作符
查看>>
《构建之法》读后感
查看>>
go语言接受者的选取
查看>>
php分页
查看>>
ORACLE Index Lookup索引访问路径总结
查看>>
SpringMVC学习指南【笔记6】JSTL标签、函数
查看>>
GPS轨迹数据集免费下载资源整理
查看>>
考研?还是直接找工作?
查看>>
ue4 蓝图物体怎么不跟着蓝图动_UE4部分蓝图
查看>>
联通4g满格但是网速慢_为什么手机4G信号明明是满格,网络却很慢,背后的真实原因?...
查看>>
bable怎么使用 eclipse_Java Web轻松学46 - Maven集成到Eclipse中
查看>>
机器人 知乎碧桂园_碧桂园机器人首降淮阳,助力城市文化旅游
查看>>
ae2020不支持的视频驱动程序_音视频PaaS平台基于Windows的抓屏技术
查看>>
图纸打印什么时候用蓝图_工程图纸为什么是蓝图?
查看>>
网页中竖的目录怎么改成横的_骨架隔墙怎么做?
查看>>
查看历史操作记录_git操作方法
查看>>
5怎么选国外节点_房子装修,床垫怎么选?这5家床垫值得买
查看>>
变成一列_Excel一列数据转多行多列,这4条函数公式可以学起来
查看>>
手机超广角拍摄软件_如何用超广角“看开一点”?OPPO官方教学,这些大片装下整个夏天...
查看>>
rip协议中周期性广播路由信息的报文_关于RIP的一点小笔记--华为
查看>>