1002 A + B Problem II [ACM刷题]
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
//这一版AC代码,由于题目对于给定的输入输出太多限定,所以代码有很多冗余的部分,
//核心思想就是使用堆栈的方法,使用三个堆栈实现了大数的加法
#include <iostream>
#include <string>
#include <stack>
#include <vector>
using namespace std;
int count = ;//声明一个count全局变量,用作进位计算
int main()
{
int i, n;
vector<string> vec1;
vector<string> vec2;
string str1, str2;
stack<char> stk1;
stack<char> stk2;
stack<int> rstk;
cin >> n;
//完成大数字符串的输入
for(i = ; i < n; ++i)
{
cin >> str1 >> str2;
vec1.push_back(str1);
vec2.push_back(str2);
}
for(i = ; i < n; ++i)
{
//将数字以char字符形式读入并push进堆栈中
for(const auto x : vec1[i])
{
stk1.push(x);
}
for(const auto y : vec2[i])
{
stk2.push(y);
}
//取堆栈元素进行加法运算
while(!stk1.empty() && !stk2.empty())
{
int temp = stk1.top() - '' + stk2.top() - '' + count;
if(temp > )
{
temp %= ;
count = ;
}
else
{
count = ;
}
rstk.push(temp);
stk1.pop();
stk2.pop();
}
//进行多余高位的push
while(!stk1.empty())
{
int temp = stk1.top() - '' + count;
if(temp > )
{
temp %= ;
count = ;
}
else
{
count = ;
}
rstk.push(temp);
stk1.pop();
}
while(!stk2.empty())
{
int temp = stk2.top() - '' + count;
if(temp > )
{
temp %= ;
count = ;
}
else
{
count = ;
}
rstk.push(temp);
stk2.pop();
}
while(count == )
{
rstk.push();
count = ;
}
cout << "Case " << i+ << ":" << endl;
cout << vec1[i] << " " << "+" << " " << vec2[i] << " = ";
//最终结果打印出来即可
while(!rstk.empty())
{
cout << rstk.top();
rstk.pop();
}
cout << endl;
while(i < n-)
{
cout << endl;
break;
}
}
return ;
}
精简代码
/*这一版是简化版的大数加法代码
*去除了一部分输入输出规则,以核心代码为主
*/
#include <iostream>
#include <string>
#include <stack>
#include <vector>
using namespace std;
int count = ;//声明一个count全局变量,用作进位计算
int main()
{
int i = ;
string str1, str2;
stack<char> stk1;
stack<char> stk2;
stack<int> rstk;
while(cin >> str1 >> str2)
{
//将数字以char字符形式读入并push进堆栈中
for(const auto x : str1)
{
stk1.push(x);
}
for(const auto y : str2)
{
stk2.push(y);
} //取堆栈元素进行加法运算
while(!stk1.empty() && !stk2.empty())
{
int temp = stk1.top() - '' + stk2.top() - '' + count;
if(temp > )
{
temp %= ;
count = ;
}
else
{
count = ;
}
rstk.push(temp);
stk1.pop();
stk2.pop();
}
//进行多余高位的push
while(!stk1.empty())
{
int temp = stk1.top() - '' + count;
if(temp > )
{
temp %= ;
count = ;
}
else
{
count = ;
}
rstk.push(temp);
stk1.pop();
}
while(!stk2.empty())
{
int temp = stk2.top() - '' + count;
if(temp > )
{
temp %= ;
count = ;
}
else
{
count = ;
}
rstk.push(temp);
stk2.pop();
}
while(count == )
{
rstk.push();
count = ;
}
cout << "Case " << i+ << ":" << endl;
cout << str1 << " " << "+" << " " << str2 << " = ";
//最终结果打印出来即可
while(!rstk.empty())
{
cout << rstk.top();
rstk.pop();
}
cout << endl << endl;
++i;
}
return ;
}
PS:网上OJ的输入输出的形式规范实在是太蛋疼了!!!
1002 A + B Problem II [ACM刷题]的更多相关文章
- 抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)
数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. while (a) //将每位数字取出来, ...
- Problem : 1002 ( A + B Problem II )
经验总结:一定要注意输出的格式,字符的空格,空行,一定要观察清楚.如本题的最后一个输出结果后面没有空行.最后代码实现的时候需要判断一下,代码如下 !=n) cout<<endl; Prob ...
- hduoj 1002 A + B Problem II
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目描述如下: A + B Problem II Time Limit: 2000/1000 M ...
- 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) ...
- 杭电acm刷题顺序
最近兴趣来了,闲暇之余,回顾大学期间刷过的杭电acm那些入门级别的题,以此巩固基础知识! 以下参考刷题顺序,避免入坑 原文传送门:https://blog.csdn.net/liuqiyao_01/a ...
- HDU 1002 A + B Problem II
A + B Problem II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted ...
- hdoj 1002 A + B Problem II
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdoj 1002 A + B Problem II【大数加法】
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1002 A + B Problem II(大整数相加)
A + B Problem II Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...
随机推荐
- sql server主动推送客户端更新数据
小谈需求: 最近工作上接到一个需求,做一个web展示数据的报表,最好能实时更新,不限制所用技术. 第一个问题:web服务器推送给浏览器新数据,一开始我想到的最快的最简单的方法就是 在web页面上js轮 ...
- java_设计模式_策略模式_Strategy pattern(2016-07-15)
感受:将算法从方法级别,提升到类级别.之后利用java多态,来切换不同的算法实现不同的功能. 在阎宏博士的<JAVA与模式>一书中开头是这样描述策略(Strategy)模式的: 策略模式属 ...
- c读写文件相关
1.打开文件: 函数原型: FILE * fopen(const char * path,const char * mode); 返回值: 文件顺利打开后,指向该流的文件指针就会被返回.如果文件打开失 ...
- 简单高效读写修改整个文本Slurp
语法: use File::Slurp; #标量环境下一次读取所有文本内容到标量中. my $text = read_file( 'filename' ) ; # 读取文本的所有行到数组中. my ...
- splice 操作符
几乎所有的数组操作都可用 splice 实现. 除了第一个参数,数组,为必须,其余的参数都不是必须的. splice ARRAY, OFFSET, LENGTH, LIST OFFSET 和 LENG ...
- centos7 开机启动某些程序的方法
针对svn,nginx每次重启后均要手工启动,好麻烦,所以考虑将其做成开机启动,做成服务好麻烦,考虑像windows 一样,放在某个启动项中完成. 打开启动文件后,发现里面文件内容如下: #!/bin ...
- (转载)无缝滚动图片的js和jquery两种写法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- css学习--inline-block详解及dispaly:inline inline-block block 三者区别精要概括
*知识储备: 内联元素:是不可以控制宽和高.margin等:并且在同一行显示,不换行. 块级元素:是可以控制宽和高.margin等,并且会换行. 1.inline-block 详解 (1)一句话就是在 ...
- Json串到json对象的转换
JSON(JavaScript Object Notation) JS对象符号 是一种轻量级的数据交换格式 JavaScript eval()函数实现 (一) 标准格式 function JsonFo ...
- SVN版本回滚~
如果你在svn上对文件进行编辑作了修改,想撤销,那么有两种方法可以还原:1) svn revert <yourfile>2) 手动删除该文件,重新执行svn up(rm <yourf ...