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 ...
随机推荐
- java_设计模式_单例模式_Singleton Pattern(2016-08-04)
概念: 单例模式确保某个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 适用场景: 在计算机系统中,线程池.缓存.日志对象.对话框.打印机.显卡的驱动程序对象常被设计成单例.这些应用都或多或 ...
- 知识库系统confluence5.8.10 安装与破解
一直对知识库体系很在意,设想这样的场景,公司历年的研发资料只要一个搜索,相关的知识点就全部摆在面前,任君取用,想一想就无限迷人,只是从10年开始,由于种种原因,终究没能好好研究一下.最近机缘巧合,可以 ...
- 如何去掉textarea右下角的灰色角标?
在css中定义: resize: none; ,这个样式同时禁用textarea调整大小
- 【技术宅4】如何把M个苹果平均分给N个小朋友
$apple=array('apple1','apple2','apple3','apple4','apple5','apple6','apple7','apple8','apple9','apple ...
- linux下gdal的python包的安装
由于python包是从C++包编译出来的,所以需要先下载源码进行编译安装.1. gdal下载http://download.osgeo.org/gdal/CURRENT/sudo ./configur ...
- 模块化编程AMD&CommonJS
为什么要模块化编程 如果JS也可以像类似python,Java使用import,引入我们想要的模块,想要什么模块,就加载什么模块,可以给前端编程带来更多的便捷,结构更加清晰明了.但是,这样做有一个前提 ...
- CodeChef FNCS
题面:https://www.codechef.com/problems/FNCS 题解: 我们考虑对 n 个函数进行分块,设块的大小为S. 每个块内我们维护当前其所有函数值的和,以及数组中每个元素对 ...
- iOS中MVC等设计模式详解
iOS中MVC等设计模式详解 在iOS编程,利用设计模式可以大大提高你的开发效率,虽然在编写代码之初你需要花费较大时间把各种业务逻辑封装起来.(事实证明这是值得的!) 模型-视图-控制器(MVC)设计 ...
- [转载]网络编辑必知常识:什么是PV、UV和PR值 zz
1.什么是pv PV(page view),即页面浏览量,或点击量;通常是衡量一个网络新闻频道或网站甚至一条网络新闻的主要指标. 高手对pv的解释是,一个访问者在24小时(0点到24点)内到底看了你网 ...
- RichEdit中插入带背景色文本的一种思路
uses RichEdit; function TextToRtf( // 将文本处理为RTF格式 mText: WideString // 输入文本 ): WideString; // 返回处理后的 ...