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 ...
随机推荐
- Oauth认证简介
Oauth是什么: 1.Oauth是一种安全认证的协议: 2.Oauth为用户资源的授权提供了一个安全的.开放而又简易的标准: 3.Oauth的授权不会使第三方触及到用户的账号信息(用户名和密码). ...
- 前端模板文件化jQuery插件 $.loadTemplates
工作中使用前端模板引擎,如 artTemplate.jsRender,来替代拼接字符串. 可是直接把模板写在页面上会带来页面臃肿,模板无法重用,与 ASP.NET等后端语言语法冲突等问题. 所以将多个 ...
- php基于数组的分页实现
关于数组的分页函数,用数组进行分页的好处是可以方便的进行联合多表查询,只需要将查询的结果放在数组中就可以了以下是数组分页的函数,函数page_array用于数组的分页,函数show_array用于分页 ...
- Android 常用网站
Android Design : http://www.sunjw.us/adchs/index.html Android Developers : http://developer.android. ...
- python自动开发之第十八天
一.JS正则 test - 判断字符串是否符合规定的正则 rep = /\d+/; rep.test("asdfoiklfasdf89asdfasdf") # true rep = ...
- 如何正确的使用uwsgi
简单的安装过程可以在这里找到,这里主要说一下如何配置uwsgi的服务,将uwsgi服务加入系统进程,你可以使用如下两种方式安装 apt-get apt-get install uwsgi 该命令会自动 ...
- WebForm
ASP开发方式 格式 <% %> C#代码可以写在里面 <%= %> 往外输出一个值,可以放一个变量,一个方法(这个方法是有返回值的直接打印到界面上去) <%@ %& ...
- Oracle 精简绿色版客户端的配置
在项目开发中常常用到Oracle.但Oracle 客户端体积很大.安装后,主要用的就1个功能:TNS配置服务名,偶尔用到SqlPlus.在开发过程中,大量使用Navicate和PL/SQL Devel ...
- 工作总结:MFC调用Windows自带新建、保存对话框代码
保存: void CExample17Dlg::OnBnClickedSaveButton() { // TODO: Add your control notification handler cod ...
- BZOJ 1207 打鼹鼠
Description 鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的.根据这个特点阿Q编写了一个打鼹鼠的游戏:在一个n*n的网格中,在某些时刻鼹鼠会在某一个网格探 ...