大数加法(STL list)
#include<iostream>
#include<list>
#include<string>
using namespace std; int main()
{
list<int>l1, l2, l3;
string s1, s2;
cin >> s1;
cin >> s2; //储存大数
for (int i = 0; i<s1.length(); i++)
l1.push_front(s1[i] - '0'); for (int i = 0; i<s2.length(); i++)
l2.push_front(s2[i] - '0'); list<int>::iterator it1, it2;
it1 = l1.begin();
it2 = l2.begin(); //进位保存
int carry = 0; //计算位数相同部分
while (it1 != l1.end() && it2 != l2.end())
{
l3.push_front((*it1 + *it2 + carry) % 10);
carry = (*it1 + *it2 + carry) / 10;
it1++;
it2++;
} list<int>::iterator it3 = (it1 == l1.end()) ? it2 : it1;
list<int>::iterator it4 = (it1 == l1.end()) ? l2.end() : l1.end(); //计算位数不相同部分
while (it3 != it4)
{
l3.push_front((*it3 + carry) % 10);
carry = (*it3 + carry) / 10;
it3++;
}
l3.push_front(carry); //输出
for (list<int>::iterator it = l3.begin(); it != l3.end(); it++)
cout << *it;
cout << endl; return 0;
}
大数加法(STL list)的更多相关文章
- 51nod 1005 大数加法
#include<iostream> #include<string> using namespace std; #define MAXN 10001 },b[MAXN]={} ...
- c#大数加法
在C#中,我们经常需要表示整数.但是,c#的基本数据类型中,最大的long也只能表示-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807之间的数 ...
- 玲珑杯1007-A 八进制大数加法(实现逻辑陷阱与题目套路)
题目连接:http://www.ifrog.cc/acm/problem/1056 DESCRIPTION Two octal number integers a, b are given, and ...
- Leetcode 67 Add Binary 大数加法+字符串处理
题意:两个二进制数相加,大数加法的变形 大数加法流程: 1.倒置两个大数,这一步能使所有大数对齐 2.逐位相加,同时进位 3.倒置两个大数的和作为输出 class Solution { public: ...
- HDU1002大数加法
大数加法 c++版: #include <map> #include <set> #include <stack> #include <queue> # ...
- java实现大数加法、乘法(BigDecimal)
之前写过用vector.string实现大数加法,现在用java的BigDecimal类,代码简单很多.但是在online-judge上,java的代码运行时间和内存大得多. java大数加法:求a+ ...
- vector、string实现大数加法乘法
理解 vector 是一个容器,是一个数据集,里边装了很多个元素.与数组最大的不同是 vector 可以动态增长. 用 vector 实现大数运算的关键是,以 string 的方式读入一个大数,然后将 ...
- 【大数加法】POJ-1503、NYOJ-103
1503:Integer Inquiry 总时间限制: 1000ms 内存限制: 65536kB 描述 One of the first users of BIT's new supercompu ...
- A + B Problem II 大数加法
题目描述: Input The first line of the input contains an integer T(1<=T<=20) which means the number ...
- NI笔试——大数加法
NI笔试: 1.找出字符串第一次出现的字符.用数组建立哈希表,然后再扫描字符串并判断次数是否为1. 2.大数加法,即字符串加法.因为之前写过乘法,就以为是乘法.然后就把乘法写上去了····= = 好了 ...
随机推荐
- win8及win8.1商店出现0X80073CF9的解决办法!
//添加appinstallagent无效的方法 http://jingyan.baidu.com/article/e52e3615a2b38f40c60c51d3.html
- C语言编程小tip
1. 随机数产生 int rand(void)产生的随机数每次运行时都是与上一次相同,若要不同,用函数srand()初始化之. void srand(unsigned int seed)初始化随机数发 ...
- hibernate子查询
对于支持子查询的数据库,Hibernate支持在查询中使用子查询.一个子查询必须被圆括号包围起来(经常是SQL聚集函数的圆括号). 甚至相互关联的子查询(引用到外部查询中的别名的子查询)也是允许的. ...
- python pandas 数据处理
pandas是基于numpy包扩展而来的,因而numpy的绝大多数方法在pandas中都能适用. pandas中我们要熟悉两个数据结构Series 和DataFrame Series是类似于数组的对象 ...
- hdu 2087 剪花布条 kmp模板题
也是kuangbin专题的 专题名字太长 不复制了…… 刚好数据结构也学了kmp 找一道题敲敲模板…… 暴力的字符串匹配是O(n*m)的时间复杂度 而kmp通过一个O(m)的预处理将字符串匹配的时间复 ...
- springmvc集成aop记录操作日志
首先说明一下,这篇文章只做了记录日志相关事宜 具体springmvc如何集成配置aop对cotroller进行拦截,请看作者的另一篇文章 http://www.cnblogs.com/guokai87 ...
- 《CSS网站布局实录》读书笔记
从Web标准.HTML标记.CSS语法基础介绍到实用技巧,事无巨细.实体书已不印刷,只能下载电子版 书的背景: 国内第一本web标准的CSS布局书,2006年9月第一版,作者李超. 环境背景: 当时主 ...
- centos7 安装openvswitch
安装 1.安装依赖包:yum -y install openssl-devel wget kernel-devel 2.安装开发工具:yum groupinstall "Developmen ...
- FD.io VPP 技术Neutron VNF vRouter 实现
在OpenStack Neutron中主要有三种网络设备,路由器(Router),负载均衡器(LB)以及VPN,其中Router作为基础网络设备起到连接子网到子网.内网到外网的作用.不同子网之间的访问 ...
- 关于PC端与手机端随着手指移动图片位置放生变化的拖拽事件
当按下鼠标时,图片随鼠标移动松开时图片回到原位 drag("div_id") function drag(node_id){ var node = document.getElem ...