vector、string实现大数加法乘法

理解 vector 是一个容器,是一个数据集,里边装了很多个元素。与数组最大的不同是 vector 可以动态增长

用 vector 实现大数运算的关键是,以 string 的方式读入一个大数,然后将字串的每一个字符 s[i] 以 int 形式赋给 vector<int> a 中的每一个元素。然后将 a[i] 和 a[j] 加起来(或者乘起来)。每两个元素加起来的结果 <= 18,乘起来的结果 <= 81。

用 string 实现大数加法的方法跟 vector 差不多,但是用 string 做大数乘法就有点麻烦,我写了一会儿没写出来。

1. 用 vector 实现大数加法:

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 #include <cstdlib>
 5
 6 //#define min(a,b) ((a>b)?(b):(a))
 7 //#define max(a,b) ((a>b)?(a):(b))
 8 using namespace std;
 9
10 void bigSum(vector<int> &a, vector<int> &b, vector<int> &sum)
11 {
12     int i, j, k, tmp;
13     if( a.size() < b.size() )
14     {
15         vector<int> vectmp=a;
16         a=b;
17         b=vectmp;
18     }
19
20     sum.assign(a.size(), 0);
21     for (i=a.size()-1, j=b.size()-1; i>=0; --i)
22     {
23         if(j>=0)
24         {
25             sum[i]=a[i]+b[j];
26             j--;
27         }
28         else sum[i]=a[i];
29     }
30
31     for (k = sum.size() - 1;   k >= 0; --k)
32     {
33         if (sum[k] > 9)
34         {
35             sum[k]-=10;
36             if(k!=0) sum[k-1]++;
37             else sum.insert(sum.begin(), 1);
38         }
39     }
40 }
41
42 int main()
43 {
44     string x,y;
45     //freopen("in.txt","r",stdin);
46     while(cin>>x>>y)
47     {
48         vector<int> a,b,c;
49         for(int i=0; i<x.length(); ++i)
50             a.push_back(x[i]-‘0‘);
51         for(int i=0; i<y.length(); ++i)
52             b.push_back(y[i]-‘0‘);
53
54         bigSum(a,b,c);
55         for(int i=0; i<c.size(); ++i)
56             cout<<c[i];
57         cout<<endl<<endl;
58     }
59     return 0;
60 }

运行:

2. string 实现大数加法:

 1 //this algorithm is from "oj-killer" of code.google.com
 2 #include <iostream>
 3 #include <cstdlib>     //freopen
 4 #include <string>    //string
 5
 6 using namespace std;
 7 string Sum(string a,string b)
 8 {
 9     if(a.length()<b.length())
10     {
11         string temp=a; a=b; b=temp;
12     }
13     int i,j;
14     for(i=a.length()-1,j=b.length()-1;i>=0;i--,j--)
15     {
16         a[i]=(a[i]+(j>=0?b[j]-‘0‘:0));
17         if(a[i]>‘9‘)
18         {
19             a[i] -=10;
20             if(i) a[i-1]++;
21             else a=‘1‘+a;
22         }
23     }
24     return a;
25 }
26 int main()
27 {
28     string s1,s2;
29     freopen("in.txt", "r", stdin);
30     //freopen("out.txt", "w", stdout);
31     while(cin>>s1>>s2)
32     {
33         cout<<"s1:\t"<<s1<<endl<<"s2:\t"<<s2<<endl;
34         cout<<"Sum:\t"<<Sum(s1,s2)<<endl<<endl;
35     }
36     return 0;
37 }

运行:

3. vector 实现大数乘法:

输入:n

输出:2^(n+1)-1

该算法来自:http://hi.baidu.com/hehui1500/item/6711a09f18590fd91e4271fc

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 using namespace std;
 5
 6 void multiply(const vector<int> &a, const vector<int> &b, vector<int> &result);
 7
 8 int main(void)
 9 {
10     int i, j, n;
11     while(cin >> n)
12     {
13         vector<int> a, b, c;
14
15         a.push_back(1);
16         b.push_back(2);
17
18         for(i = 0; i <= n; ++i)
19         {
20             c.assign(a.size() + b.size() - 1, 0);
21             multiply(a, b, c);
22             a = c;
23         }
24
25         for (i = 0; i < a.size() - 1; ++i)
26             cout << a[i];
27         cout << c[a.size() - 1] - 1;
28         cout << endl;
29     }
30     return 0;
31 }
32
33 void multiply(const vector<int> &a, const vector<int> &b, vector<int> &result)
34 {
35     int i, j, k;
36     int tmp;
37
38     for (i = 0; i < a.size(); ++i)
39     {
40         k = i;
41         for (j = 0; j < b.size(); ++j)
42             result[k++] += a[i] * b[j];
43     }
44
45     for (k = result.size() - 1;   k >= 0; --k)
46     {
47         if (result[k] > 9)
48         {
49             if (k != 0)
50             {
51
52                 result[k - 1] += result[k] / 10;
53                 result[k] %= 10;
54             }
55             else
56             {
57                 tmp = result[k] / 10;
58                 result[k] %= 10;
59                 result.insert(result.begin(), tmp);
60             }
61         }
62     }
63 }

运行:

原文地址:https://www.cnblogs.com/Pjson/p/8325890.html

时间: 2024-08-29 11:40:43

vector、string实现大数加法乘法的相关文章

大数加法乘法

大数加法乘法: 1 /* 2 2015.4 3 大数加法,乘法 4 5 */ 6 #include <iostream> 7 #include <string> 8 #include <vector> 9 10 using namespace std; 11 #define MAX 99 12 #define MAXM 200 13 14 void BigNumAdd() 15 { 16 char strA[MAX], strB[MAX]; 17 cout <&l

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

北大的ACM 1001 poj.org/problem?id=1001 代码纯手动编写 - - 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 5 6 class BigNumber 7 { 8 struct BigNumberNode 9 { 10 BigNumberNode():n(0), prev(NULL), next(NULL){} 11 BigNumberNode(int N)

大数加法、大数乘法

大数加法 hdu1002 #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <sstream> #include <algorithm> #include <set> #include <map> #include <vector> #i

大数加法、乘法

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

大数加法、减法、乘法

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

HDU 1250 Hat&#39;s Fibonacci (递推、大数加法、string)

Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14776    Accepted Submission(s): 4923   Problem Description A Fibonacci sequence is calculated by adding the previous two members

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

LeetCode-Multiply Strings实现大数的乘法

Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 题目当中主要是需要两个大数的乘法,一般大数都是用字符串进行保存 代码比较简单,主要是利用了一个vector反向的存储了计算的结果,然后reverse到string当中进行输出 这里当然可

POJ 2756 Autumn is a Genius 使用string的大数加减

本题就是说一个小神童,能计算加减法. 不过题目知识说这个小神童,到底有多神,要我们自己发现. 因为最后给出的数据非常非常巨大,听说接近50k就是超过50000个数位相加,可想而知他多神. 看来题目也是考IQ啊! 如果以为是超级水题,按照一般加减法做,肯定是WA了. 这里给出使用string的加减法运算,因为string是长度可增可减的,所以不管是多少位,只要内存支持,那么本算法都可以支持了.也可以使用vector这些容器.不过string应该更加省点内存. 注意: POJ比较讨厌的就是不支持C+