这一段时间一直都在刷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. SQL三大范式

    第一范式:确保每列的原子性. 如果每列(或者每个属性)都是不可再分的最小数据单元(也称为最小的原子单元),则满足第一范式. 例如:顾客表(姓名.编号.地址.……)其中"地址"列还可 ...

  2. Spring中的创建与销毁

    在bean中添加属性init-method="方法名" destroy-method="方法名" init-method        该方法是由spring容 ...

  3. 仿小米网jQuery全屏滚动插件fullPage.js

    演 示 下 载   简介 如今我们经常能见到全屏网站,尤其是国外网站.这些网站用几幅很大的图片或色块做背景,再添加一些简单的内容,显得格外的高端大气上档次.比如 iPhone 5C 的介绍页面,QQ浏 ...

  4. 中级Perl第二章习题

    2. 4. 1. 习题1 [15 分钟] 写一个程序从命令行取一个文件清单, 然后用grep 把那些文件大小在1000 字节以内的文件找出来.用map 把这个清单里的每个字串前加四个空格并在 字串后面 ...

  5. shell调用shell

    在默认条件下,执行shell文件会出现permission denied报错,一般是没有可执行权限.用chmod修改权限 chomd 777 score.sh   //把所有权限给aa文件 777代表 ...

  6. 前端模板文件化jQuery插件 $.loadTemplates

    工作中使用前端模板引擎,如 artTemplate.jsRender,来替代拼接字符串. 可是直接把模板写在页面上会带来页面臃肿,模板无法重用,与 ASP.NET等后端语言语法冲突等问题. 所以将多个 ...

  7. python模块之socket

    43.python模块之socket:       Python在网络通讯方面功能强大,学习一下Socket通讯的基本方式 UDP通讯: Server: import socket port=8081 ...

  8. javascript定义类的方法总结

    1.构造函数法 类是对象的模板,定义了对象共有的方法属性数据 等,在javascript中一个函数就是一个对象,也可以看做一个类的构造方法. 所以我们可以像以下方式定义类: //1.经典的构造方法 Q ...

  9. HashMap在Android和Java中的不同实现

    起因 今天在项目中遇到一个很"奇葩"的问题.情况大致是这样的:Android终端和服务器(Spring),完全相同的字符串键值对放入HashMap中竟然顺序不一样,这直接导致了服务 ...

  10. I题 - A+B for Input-Output Practice (VIII)

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description You ...