vector、string实现大数加法乘法
理解 vector 是一个容器,是一个数据集,里边装了很多个元素。与数组最大的不同是 vector 可以动态增长。
用 vector 实现大数运算的关键是,以 string 的方式读入一个大数,然后将字串的每一个字符 s[i] 以 int 形式赋给 vector<int> a 中的每一个元素。然后将 a[i] 和 a[j] 加起来(或者乘起来)。每两个元素加起来的结果 <= 18,乘起来的结果 <= 81。
用 string 实现大数加法的方法跟 vector 差不多,但是用 string 做大数乘法就有点麻烦,我写了一会儿没写出来。
1. 用 vector 实现大数加法:
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib> //#define min(a,b) ((a>b)?(b):(a))
//#define max(a,b) ((a>b)?(a):(b))
using namespace std; void bigSum(vector<int> &a, vector<int> &b, vector<int> &sum)
{
int i, j, k, tmp;
if( a.size() < b.size() )
{
vector<int> vectmp=a;
a=b;
b=vectmp;
} sum.assign(a.size(), );
for (i=a.size()-, j=b.size()-; i>=; --i)
{
if(j>=)
{
sum[i]=a[i]+b[j];
j--;
}
else sum[i]=a[i];
} for (k = sum.size() - ; k >= ; --k)
{
if (sum[k] > )
{
sum[k]-=;
if(k!=) sum[k-]++;
else sum.insert(sum.begin(), );
}
}
} int main()
{
string x,y;
//freopen("in.txt","r",stdin);
while(cin>>x>>y)
{
vector<int> a,b,c;
for(int i=; i<x.length(); ++i)
a.push_back(x[i]-'');
for(int i=; i<y.length(); ++i)
b.push_back(y[i]-''); bigSum(a,b,c);
for(int i=; i<c.size(); ++i)
cout<<c[i];
cout<<endl<<endl;
}
return ;
}
运行:

2. string 实现大数加法:
//this algorithm is from "oj-killer" of code.google.com
#include <iostream>
#include <cstdlib> //freopen
#include <string> //string using namespace std;
string Sum(string a,string b)
{
if(a.length()<b.length())
{
string temp=a; a=b; b=temp;
}
int i,j;
for(i=a.length()-,j=b.length()-;i>=;i--,j--)
{
a[i]=(a[i]+(j>=?b[j]-'':));
if(a[i]>'')
{
a[i] -=;
if(i) a[i-]++;
else a=''+a;
}
}
return a;
}
int main()
{
string s1,s2;
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while(cin>>s1>>s2)
{
cout<<"s1:\t"<<s1<<endl<<"s2:\t"<<s2<<endl;
cout<<"Sum:\t"<<Sum(s1,s2)<<endl<<endl;
}
return ;
}
运行:

3. vector 实现大数乘法:
输入:n
输出:2^(n+1)-1
该算法来自:http://hi.baidu.com/hehui1500/item/6711a09f18590fd91e4271fc
#include <iostream>
#include <vector>
#include <string>
using namespace std; void multiply(const vector<int> &a, const vector<int> &b, vector<int> &result); int main(void)
{
int i, j, n;
while(cin >> n)
{
vector<int> a, b, c; a.push_back();
b.push_back(); for(i = ; i <= n; ++i)
{
c.assign(a.size() + b.size() - , );
multiply(a, b, c);
a = c;
} for (i = ; i < a.size() - ; ++i)
cout << a[i];
cout << c[a.size() - ] - ;
cout << endl;
}
return ;
} void multiply(const vector<int> &a, const vector<int> &b, vector<int> &result)
{
int i, j, k;
int tmp; for (i = ; i < a.size(); ++i)
{
k = i;
for (j = ; j < b.size(); ++j)
result[k++] += a[i] * b[j];
} for (k = result.size() - ; k >= ; --k)
{
if (result[k] > )
{
if (k != )
{ result[k - ] += result[k] / ;
result[k] %= ;
}
else
{
tmp = result[k] / ;
result[k] %= ;
result.insert(result.begin(), tmp);
}
}
}
}
运行:

vector、string实现大数加法乘法的更多相关文章
- sdut2613(This is an A+B Problem)大数加法(乘法)
#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>u ...
- [acm 1001] c++ 大数加法 乘法 幂
北大的ACM 1001 poj.org/problem?id=1001 代码纯手动编写 - - #include <iostream> #include <cstdio> #i ...
- java实现大数加法、乘法(BigDecimal)
之前写过用vector.string实现大数加法,现在用java的BigDecimal类,代码简单很多.但是在online-judge上,java的代码运行时间和内存大得多. java大数加法:求a+ ...
- HDU1002大数加法
大数加法 c++版: #include <map> #include <set> #include <stack> #include <queue> # ...
- Hat's Fibonacci(大数加法+直接暴力)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1250 hdu1250: Hat's Fibonacci Time Limit: 2000/1000 M ...
- PAT 甲级 1065. A+B and C (64bit) (20) 【大数加法】
题目链接 https://www.patest.cn/contests/pat-a-practise/1065 思路 因为 a 和 b 都是 在 long long 范围内的 但是 a + b 可能会 ...
- poj3535 A+B (大数加法)
A+B Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 811 Accepted: 371 Description The ...
- 大数高精度加减乘除 51nod 1005 大数加法
1005 大数加法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B ...
- 51nod 1005 大数加法
#include<iostream> #include<string> using namespace std; #define MAXN 10001 },b[MAXN]={} ...
随机推荐
- [C/C++]在头文件中使用static定义变量意味着什么
文章出处:http://www.cnblogs.com/zplutor/ 看到有一位同学在头文件中这么写: static const wchar_t* g_str1 = - static const ...
- C#操作Excel基本操作
/// using Microsoft.Office.Core; using Microsoft.Office.Interop.Excel; using System.IO; using System ...
- Lucene 4.0
关于4.0的Update Index ,Create Index /* * Create Index */ public static void createIndex() throws IOExc ...
- Sql 执行删除修改之前添加备份
backyw备份滴数据库名称,w20151124sendmaster 表名称 select * into backyw..w20151124sendmaster from Logistics.E ...
- 防DDOS攻击
/ip firewall filter add chain=forward connection-state=new action=jump jump-target=block-ddos add ch ...
- uploadify 上传
本来想做一套上传公用的组建的,里面包含文件转码等功能,看来这些都只能后来一步一步加上了,先写下来... 1,引入脚本等 @{ Layout = null; } <!DOCTYPE html> ...
- php服务器探针
<?php /* ---------------------------------------------------- */ /* 程序名称: PHP探针-Yahei /* 程序功能: 探测 ...
- centos 6.4 apache开启gzip方法
系统概况,主机CentOS6.4 Apache2.4 php5.3.6 mysql5.5 开始:首先得确认apache是否已经加载了mod_deflate模块 1.httpd -M 在结果中查看是否 ...
- Android存储机制之Preference
Preference提供了一种轻量级的数据存取方法,主要是数据比较少的配置信息.它以键值对的方式将数据保存在一个XML配置文件中. 使用Preference方式来存取数据,用到了SharedPrefe ...
- 面试问到:JDBC、hibernate、ibati
一.JDBC.Connection(连接) 优点:运行高效.快捷. 缺点:代码多.异常多.不支持跨平台. 二.ibatis 1.根据jdbc的基本建立连接. 2.通过anntation+xml.jav ...