大数加法(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;
}

  

时间: 2024-08-04 21:00:02

大数加法(STL list)的相关文章

C语言 &#183; 大数加法

算法提高 大数加法 时间限制:1.0s   内存限制:256.0MB 问题描述 输入两个正整数a,b,输出a+b的值. 输入格式 两行,第一行a,第二行b.a和b的长度均小于1000位. 输出格式 一行,a+b的值. 样例输入 42 样例输出 6 1 #include<stdio.h> 2 #include<string.h> 3 int main() 4 { 5 char a[1001],b[1001]; 6 int s[1001],d[1001]; 7 scanf("

NI笔试——大数加法

NI笔试: 1.找出字符串第一次出现的字符.用数组建立哈希表,然后再扫描字符串并判断次数是否为1. 2.大数加法,即字符串加法.因为之前写过乘法,就以为是乘法.然后就把乘法写上去了····= = 好了,看一下加法的思路. 要不要太简单,用俩数组,先把字符串每个位转换成数字存到这俩数组里,然后对每一位进行加和. 代码是拿别人的.= = void Add(char s1[],char s2[]) //需要两个字符串参数&&无返回值 { int num1[M],nm2[M]; int i,j;

大数加法、乘法

1 #include<iostream> 2 3 #include<stdio.h> 4 5 #include<string.h> 6 7 #define Len 3000//大数的长度 8 9 using namespace std; 10 11 int Input (char n[])//将大数读入的函数 12 13 { 14 15 char s[Len]; 16 17 int i,l; 18 19 20 21 for(i=0; i<Len; i++) n[i

A + B Problem II(大数加法)

一直格式错误,不想改了,没A 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <stdlib.h> 5 using namespace std; 6 7 int main() 8 { 9 int T,K=0; 10 scanf("%d",&T); 11 char a[1002],b[1002]; 12 int ta[1002],

大数加法、减法、乘法

大数四则运算--C++实现 大数处理--c++实现 本课题来自我的c++编程作业,文章利用大数处理类,类名:hugeNumber来对大数(编译器自定义的数值类型无法处理的数)进行四则运算(大数加法.大数减法及大数乘法的运算,除暂时没实现)和按精度四舍五入,自定义科学计数法等.内容广泛涉及运算符重载.字符连接.字符加减和字符乘除等作者原创函数.重要提示:本文涉及的所有函数使用的进制皆为10进制.(备注:已将该博客搬迁至CSDN) 一.解题思路 1 核心思想 文章用hugeNumber类对大数进行操

c#大数加法

在C#中,我们经常需要表示整数.但是,c#的基本数据类型中,最大的long也只能表示-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807之间的数.货币类型也不是无限大.如果我们需要表示更大的数,就需要用到一定的算法来完成. 这次,我和大家一起讨论一下c#的大数运算之加法. 这次,我们只考虑正数的整数加法. 我们的代码要封装到一个结构里面.这个结构的结构先摆出来. public struct BigInt { public int[] num

大数加法 模板

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents #include<cstdio> #include<cstring> void fan(char s[]) { char t; int i,j; for(i = 0,j = strlen(s)-1;i <= j;i++,j--) { t=s[i];s[i]=s[j];s[j]=t; } } int main() { int i,j,n,p=0,g=0,h=1,k

POJ 1053 Integer Inquiry (大数加法,还是Java大法好)

Integer Inquiry Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32674   Accepted: 12789 Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he

Hat&#39;s Fibonacci(大数加法+直接暴力)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1250 hdu1250: Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9442    Accepted Submission(s): 3096 Problem Description A Fibonacci

51 Nod 1005 大数加法【Java大数乱搞,python大数乱搞】

1005 大数加法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 <= 10000 需注意:A B有可能为负数) Output 输出A + B Input示例 68932147586 468711654886 Output示例 537643802472 题目链接:http://www.51nod.com/onlineJudge/questionCode.html#