欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

C++怎么實(shí)現(xiàn)大整數(shù)乘法

這篇文章將為大家詳細(xì)講解有關(guān)C++怎么實(shí)現(xiàn)大整數(shù)乘法,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

專(zhuān)注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)、網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)岷縣免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了千余家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

算法競(jìng)賽入門(mén)經(jīng)典 這本書(shū)并沒(méi)有對(duì)大數(shù)乘法實(shí)現(xiàn),所以自己補(bǔ)充了一下,乘法的實(shí)現(xiàn)很簡(jiǎn)單,就是再其數(shù)據(jù)結(jié)構(gòu)基礎(chǔ)上把每寬為8位的十進(jìn)制數(shù)看成多項(xiàng)式的系數(shù),vector的下標(biāo)看成多項(xiàng)式的指數(shù),然后再對(duì)應(yīng)相乘相加就可以了,注意系數(shù)超過(guò)8位 將超八位的補(bǔ)分進(jìn)位。

我這里是笛卡爾相乘。一般來(lái)說(shuō)是夠用的。

但其實(shí)多項(xiàng)式乘法算法還有很多更高效的。

#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
struct BigInteger{
  static const int BASE = 100000000;
  static const int WIDTH = 8;
  vector<int> s;
 
  BigInteger operator = (const string& str){
    s.clear();
    int x, len=(str.length()-1)/WIDTH+1;
    for(int i=0;i<len;i++){
      int r=str.length()-i*WIDTH;
      int l=max(0,r-WIDTH);
      sscanf(str.substr(l,r-l).c_str(),"%d",&x);
      s.push_back(x);
    }
    return *this;
  }
 
  BigInteger operator * (const BigInteger& b){
    BigInteger c;
    int lena=this->s.size(),lenb=b.s.size(),lenc=lena+lenb-1;
    LL *buf =new LL[lenc+1];
    for(int i=0;i<lenc+1;i++)buf[i]=0;
    for(int i=0;i<lena;i++)
      for(int j=0;j<lenb;j++){
        buf[i+j]+=(this->s[i])*((LL)b.s[j]);
        buf[i+j+1]+=buf[i+j]/BASE;
        buf[i+j]=buf[i+j]%BASE;
      }
    for(int i=0;i<lenc;i++)c.s.push_back(buf[i]);
    if(buf[lenc])c.s.push_back(buf[lenc]);
    return c;
  }
 
  BigInteger operator * (const int& x){
    char c[128];
    sprintf(c,"%d",x);
    string str(c);
    BigInteger res;
    res=str;
    return *this*res;
  }
};
 
ostream& operator<<(ostream& out,const BigInteger& b){
  int len=b.s.size();
  out<<b.s[len-1];
  for(int i=len-2;i>=0;i--){
    int buf=b.s[i],h=8;
    while(buf>0){buf/=10;h--;}
    for(int j=0;j<h;j++)out<<0;
    if(b.s[i])out<<b.s[i];
  }
  return out;
}
 
int main()
{
  int n;BigInteger b;
  b="1000000000000";
  cout<< b<<endl;
  cout<< (b*b)*4*b*b <<endl;
}

關(guān)于“C++怎么實(shí)現(xiàn)大整數(shù)乘法”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

名稱(chēng)欄目:C++怎么實(shí)現(xiàn)大整數(shù)乘法
文章源于:http://chinadenli.net/article16/ihopdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)網(wǎng)站導(dǎo)航響應(yīng)式網(wǎng)站靜態(tài)網(wǎng)站外貿(mào)建站面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作