#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)的更多相关文章

  1. 51nod 1005 大数加法

    #include<iostream> #include<string> using namespace std; #define MAXN 10001 },b[MAXN]={} ...

  2. c#大数加法

    在C#中,我们经常需要表示整数.但是,c#的基本数据类型中,最大的long也只能表示-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807之间的数 ...

  3. 玲珑杯1007-A 八进制大数加法(实现逻辑陷阱与题目套路)

    题目连接:http://www.ifrog.cc/acm/problem/1056 DESCRIPTION Two octal number integers a, b are given, and ...

  4. Leetcode 67 Add Binary 大数加法+字符串处理

    题意:两个二进制数相加,大数加法的变形 大数加法流程: 1.倒置两个大数,这一步能使所有大数对齐 2.逐位相加,同时进位 3.倒置两个大数的和作为输出 class Solution { public: ...

  5. HDU1002大数加法

    大数加法 c++版: #include <map> #include <set> #include <stack> #include <queue> # ...

  6. java实现大数加法、乘法(BigDecimal)

    之前写过用vector.string实现大数加法,现在用java的BigDecimal类,代码简单很多.但是在online-judge上,java的代码运行时间和内存大得多. java大数加法:求a+ ...

  7. vector、string实现大数加法乘法

    理解 vector 是一个容器,是一个数据集,里边装了很多个元素.与数组最大的不同是 vector 可以动态增长. 用 vector 实现大数运算的关键是,以 string 的方式读入一个大数,然后将 ...

  8. 【大数加法】POJ-1503、NYOJ-103

    1503:Integer Inquiry 总时间限制:  1000ms 内存限制:  65536kB 描述 One of the first users of BIT's new supercompu ...

  9. A + B Problem II 大数加法

    题目描述: Input The first line of the input contains an integer T(1<=T<=20) which means the number ...

  10. NI笔试——大数加法

    NI笔试: 1.找出字符串第一次出现的字符.用数组建立哈希表,然后再扫描字符串并判断次数是否为1. 2.大数加法,即字符串加法.因为之前写过乘法,就以为是乘法.然后就把乘法写上去了····= = 好了 ...

随机推荐

  1. Java学习笔记-Json

    //先导入gson到lib,add build path //2015年5月5日22:02:37 package com.alfredsun.thread; import com.google.gso ...

  2. Hololens生成与安装(旁加载)应用

    Hololens生成应用的几种方式: 一:HoloToolkit编辑器生成appx应用 二:Vistul Studio 2015 创建应用 旁加载概述: 你可以将应用旁加载到你的设备,而无需将它们提交 ...

  3. android studio修改项目包名

    公司项目都是用eclipse开发的,但是android studio开发已经是大势所趋了,所以在闲暇之余使用了一下androidstudio,这里对androidstudio更改项目包名做一下总结,因 ...

  4. python3 验证用户名密码

    输入用户名,密码,匹配通过,不匹配报错 import getpass user = input('input username: ') pwd = getpass.getpass('input pas ...

  5. awakeFromNib、initWithCoder、initWithFrame三者区别

    (1)awakeFromNib和initWithCoder:差别awakeFromNib 从xib或者storyboard加载完毕就会调用initWithCoder: 只要对象是从文件解析来的,就会调 ...

  6. NGINX----源码阅读----init配置脚本

    /auto/init init脚本负责初始化各种目录环境变量值. 1.make文件.源文件.头文件.配置头文件路径变量初始化. NGX_MAKEFILE=$NGX_OBJS/Makefile NGX_ ...

  7. Java 年月日 日期加减

    public static String DATE_YEAR="YEAR";//年 public static String DATE_MONTH="MONTH" ...

  8. NYOJ 299

    (前言:这是一道关于矩阵快速幂的问题,介绍矩阵快速幂之前,首先看"快速幂"问题. 在前面的博客里有记录到快速幂取模算法,不过总体的思想总是和取模运算混淆在一起,而忽略了" ...

  9. POJ 3414 Pots(BFS)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description You are g ...

  10. mouseover和this的巧用

    mouseover & mouseout 的问题 在JS中,使用mouseover & mouseout会有触发多次的问题,这里Jquery有了替代的新属性 mouseover == ...