欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

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

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊: 次

算法競(jìng)賽入門經(jīng)典 這本書并沒(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;
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:C++類繼承 繼承后函數(shù)的值實(shí)現(xiàn)詳解

欄    目:C語(yǔ)言

下一篇:C++使用string的大數(shù)快速模冪運(yùn)算(6)

本文標(biāo)題:C++實(shí)現(xiàn)大整數(shù)乘法

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/194.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有