zoukankan      html  css  js  c++  java
  • 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:	"<<s1<<endl<<"s2:	"<<s2<<endl;
    34         cout<<"Sum:	"<<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 }
    复制代码

    运行:

  • 相关阅读:
    Linux客户/服务器程序设计范式——阿帕奇服务器(多进程)
    Linux客户/服务器程序设计范式2——并发服务器(进程池)
    封装readn
    C++学习之路: 函数适配器
    C++学习之路: 智能指针入门
    C++学习之路: 单例模板
    C++学习之路: 左值&右值 的讨论 和 ”move“ 值传递方式
    C++学习之路: 特殊操作符->的重载
    C++学习之路: 模板函数
    C++学习之路: 时间戳 封装成类
  • 原文地址:https://www.cnblogs.com/Pjson/p/8325890.html
Copyright © 2011-2022 走看看