大整数加法
来源:互联网 发布:风险评估矩阵中 编辑:程序博客网 时间:2024/06/16 13:33
6:大整数加法
- 查看
- 提交
- 统计
- 提问
- 总时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
求两个不超过200位的非负整数的和。
- 输入
- 有两行,每行是一个不超过200位的非负整数,可能有多余的前导0。
- 输出
- 一行,即相加后的结果。结果里不能有多余的前导0,即如果结果是342,那么就不能输出为0342。
- 样例输入
2222222222222222222233333333333333333333
- 样例输出
55555555555555555555
- 来源
- 程序设计实习2007
- 查看
- 提交
- 统计
- 提问using namespace std;
#include<iostream>
#include<iomanip>
#include<stack>
#include<string>
int main()
{
stack<int> a, b, c;
int f;
int flag=0;
string g,m;
getline(cin,g);
auto p = g.begin();
while (p!=g.end())
{
f = *p-'0';
a.push(f);
p++;
}
getline(cin, m);
p =m.begin();
while (p != m.end())
{
f = *p-'0';
b.push(f);
p++;
}
while ((a.size() != 0) && (b.size() != 0))
{
f = a.top() + b.top() + flag;
if (f >= 10)
flag = 1;
else flag = 0;
c.push((f) % 10);
a.pop();
b.pop();
}
while (!a.empty())
{
f = a.top() + flag;
if (f >= 10)
flag = 1;
else flag = 0;
c.push((f) % 10);
a.pop();
}
while (!b.empty())
{
f = b.top() + flag;
if (f >= 10)
flag = 1;
else flag = 0;
c.push((f) % 10);
b.pop();
}
if (flag == 1)
c.push(1);
while ((!c.empty())&&(c.top() == 0))
c.pop();
if (c.empty())
cout << 0;
while (!c.empty())
{
f = c.top();
cout << f;
c.pop();
}return 0;
} - 虽然只是第六题,但应该算是堆栈的典型应用了,代码略长,不知道哪里可以精简一下
- 全局题号
- 1982
- 提交次数
- 190
- 尝试人数
- 58
- 通过人数
- 50
你的提交记录
#结果时间4Accepted05-083Runtime Error05-082Wrong Answer05-081Wrong Answer05-08 0 0
- 大整数加法
- 大整数加法
- 高精度 大整数加法
- 大整数加法
- 大整数加法
- 大整数加法
- 大整数加法
- 大整数加法计算
- zoj1292大整数加法
- 大整数加法
- 大整数加法
- 大整数加法函数
- 大整数加法
- 大整数加法
- 大整数加法
- 大整数加法
- 大整数加法
- 【算法】大整数加法
- HBCTF第二场WP
- Windows驱动(2)-Driver安装调试报错security_init_cookie&蓝屏
- 搭建samba服务器实现Linux磁盘或文件夹映射为 Windows网络磁盘
- android插件化(Binder Hook)
- visual studio2012上运行c程序
- 大整数加法
- Leetcode学习(25)—— Add Two Numbers
- HBCTF第三场WP
- Spring boot 集成Axis1.4 ,使用wsdd文件发布webservice
- 智能优化算法概述
- Android视频点播-边播边缓存-方案
- android中使用Retrofit请求数据后,数字过大保存为科学计数法的问题
- MyBatis框架学习整理PART 1
- 近期总结