理解 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实现大数加法乘法的更多相关文章

  1. sdut2613(This is an A+B Problem)大数加法(乘法)

    #include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>u ...

  2. [acm 1001] c++ 大数加法 乘法 幂

    北大的ACM 1001 poj.org/problem?id=1001 代码纯手动编写 - - #include <iostream> #include <cstdio> #i ...

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

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

  4. HDU1002大数加法

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

  5. Hat's Fibonacci(大数加法+直接暴力)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1250 hdu1250: Hat's Fibonacci Time Limit: 2000/1000 M ...

  6. PAT 甲级 1065. A+B and C (64bit) (20) 【大数加法】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1065 思路 因为 a 和 b 都是 在 long long 范围内的 但是 a + b 可能会 ...

  7. poj3535 A+B (大数加法)

    A+B Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 811   Accepted: 371 Description The ...

  8. 大数高精度加减乘除 51nod 1005 大数加法

    1005 大数加法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B ...

  9. 51nod 1005 大数加法

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

随机推荐

  1. linux传送文件至服务器

    scp安全文件拷贝(基于ssh的登陆)     1.你想把本地/home下的文件linux.tar.gz传送至远端服务器10.108.125.30,远端服务器的账号名为name,保存至服务器/home ...

  2. Centos6.4版本下搭建LAMP环境

    Centos6.4版本下搭建LAMP环境 配置yum mkdir/mnt/cdrom mount/dev/cdrom  /mnt/cdrom 装载光盘 vi /etc/yum.repos.d/Cent ...

  3. MotionEvent中getX()和getRawX()的区别

    http://blog.csdn.net/ztp800201/article/details/17218067 public class Res extends Activity implements ...

  4. MongoDB的分组统计 group

    mongodb中的分组聚合用$group,而且处理的最大数据量为100M如果超出需要写入到磁盘,使用格式如下: { $group: { _id: <expression>, <fie ...

  5. 画了一张PHPCMSV9的运行流程思维导图

    转载:http://www.cnblogs.com/fuyunbiyi/archive/2012/03/12/2391253.html

  6. 初级jQuery的使用

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  7. js中的数组

    上网查了一下,js中的数组包含的内容还真不少.先给出两个学习的链接: w3school链接:http://www.w3school.com.cn/js/js_obj_array.asp 博客园链接:h ...

  8. oracle11g关于表空间的问题

    1.oracle11g默认的块大小为8K  每个表空间里面的单个数据文件最大为32G   (2^22-1) *4k   最多可以放1024个单个文件    SQL> show parameter ...

  9. WPF-控件-ControlTemplate生成的控件

    <Window x:Class="由ControlTemplate生成的控件.MainWindow" xmlns="http://schemas.microsoft ...

  10. VirtualBox内Linux系统与Windows共享文件夹

    在日常工作或学习中我们经常需要在一台电脑上同时使用Windows和Linux(这里以Ubuntu为例)两个系统,我们通常的做法有两种: 一种安装双系统(双系统的安装方法经验里已经有很多,大家可以去参照 ...