这一段时间一直都在刷OJ,这里建一个博客合集,用以记录和分享算法学习的进程。
 
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 CODE
//这一版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刷题]的更多相关文章

  1. 抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)

    数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. while (a) //将每位数字取出来, ...

  2. Problem : 1002 ( A + B Problem II )

    经验总结:一定要注意输出的格式,字符的空格,空行,一定要观察清楚.如本题的最后一个输出结果后面没有空行.最后代码实现的时候需要判断一下,代码如下 !=n) cout<<endl; Prob ...

  3. hduoj 1002 A + B Problem II

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目描述如下: A + B Problem II Time Limit: 2000/1000 M ...

  4. 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) ...

  5. 杭电acm刷题顺序

    最近兴趣来了,闲暇之余,回顾大学期间刷过的杭电acm那些入门级别的题,以此巩固基础知识! 以下参考刷题顺序,避免入坑 原文传送门:https://blog.csdn.net/liuqiyao_01/a ...

  6. HDU 1002 A + B Problem II

    A + B Problem II   Time Limit: 1000MS      Memory Limit: 65536K Total Submissions: 16104    Accepted ...

  7. hdoj 1002 A + B Problem II

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. hdoj 1002 A + B Problem II【大数加法】

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. HDU 1002 A + B Problem II(大整数相加)

    A + B Problem II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

随机推荐

  1. QT QSettings 操作(导入导出、保存获取信息)*.ini文件详解

    1.QSettings基本使用 1.1.生成.ini文件,来点实用的代码吧. QString fileName;fileName = QCoreApplication::applicationDirP ...

  2. 1. mybatis批量插入数据

    通过list <insert id="saveByList" useGeneratedKeys="true" parameterType="ja ...

  3. linux搜索命令

    1. find find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件. find的使用格式如下: $ find <指定目录> <指定条件> <指定动作> ...

  4. JS获取浏览器可视区域的尺寸

    所谓可视区域是指能看得见的区域,即在浏览器中能看到页面的区域(高度与宽度).刚刚使用 document.body.clientHeight 来获取可视区域的高度得到的却是整个文档的高度,然后在cnbl ...

  5. jQuery之文本框得失焦点

    版本一 css代码部分: .focus { border: 1px solid #f00; background: #fcc; } 当焦点获得时,添加focus样式,添加边框,并改背景色为#fcc h ...

  6. WPF WebBrowser 不可见问题的解析[转]

    问题概述: 1.在Xaml中加入WebBrowser(不论是WPF中的控件,还是Winform中的控件) 2.设置Window Background="Transparent" A ...

  7. VB.NET中LINQ TO List泛型查询语句(分组,聚合函数)

    Public Class LinqToList 'LINQ在C#中使用比较方便,但是在VB中使用比较麻烦,复杂,和C#用法并不太一样 Dim listNew As List(Of Product) = ...

  8. dedecms自定义表单提交成功如何返回当前页面

    在plus/diy.php找到showmsg($bkmsg, $goto);改成showmsg($bkmsg, -1);

  9. Win7系统下完全删除Mysql

    今天不知为什么Mysql服务器突然连接不上,于是胡乱折腾了一番,导致最后不得不重新安装Mysql.安装不成功,服务器起不来,就是最后那步的时候服务器启动不了,这是因为Mysql在卸载的时候没有彻底卸载 ...

  10. php判断手机移动设备访问

    <?php function isMobile() { // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 if (isset ($_SERVER['HTTP_X_WAP_PROF ...