2009-12-28 144 views
4

我正在準備面試的問題不是作業。有一個關於如何將非常非常長的整數倍數的問題。任何人都可以在C++中提供任何源代碼來學習?我試圖通過學習其他解決方案來改善自己,從而縮小我和他人之間的差距。長整數乘法

非常感謝!

對不起,如果你認爲這是不正確的地方問這樣的問題。

+1

表示整爲一個數字陣列。乘以一個標量是微不足道的;它是當你乘以兩個大整數,你需要考慮一下。 – 2009-12-28 19:27:06

+0

這篇文章從概念上說明你將如何乘以大整數http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=641 – 2009-12-28 19:27:41

回答

4

對於C++,您可以使用GNU Multiple Precision Arithmetic Library

如果你只是想一個簡單的方法來乘以龐大的數字(整數),在這裏你是:

#include<iostream> 

#include<string> 
#include<sstream> 
#define SIZE 700 

using namespace std; 



class Bignum{ 

    int no[SIZE]; 


    public: 

     Bignum operator *(Bignum& x){ // overload the * operator 
     /* 
      34 x 46 
      ------- 
       204   // these values are stored in the 
       136   // two dimensional array mat[][]; 
      ------- 
      1564 // this the value stored in "Bignum ret" 
     */        
    Bignum ret;    
    int carry=0; 
    int mat[2*SIZE+1][2*SIZE]={0}; 
    for(int i=SIZE-1;i>=0;i--){ 
     for(int j=SIZE-1;j>=0;j--){ 
      carry += no[i]*x.no[j]; 
      if(carry < 10){ 
       mat[i][j-(SIZE-1-i)]=carry; 
       carry=0; 
      } 
      else{ 
       mat[i][j-(SIZE-1-i)]=carry%10; 
       carry=carry/10; 
      } 
     } 
    } 
    for(int i=1;i<SIZE+1;i++){ 
     for(int j=SIZE-1;j>=0;j--){ 
      carry += mat[i][j]+mat[i-1][j]; 

      if(carry < 10){ 

       mat[i][j]=carry; 

       carry=0; 

      } 

      else{ 

       mat[i][j]=carry%10; 

       carry=carry/10; 

      } 
     } 
    } 
    for(int i=0;i<SIZE;i++) 
     ret.no[i]=mat[SIZE][i]; 
    return ret; 
} 

Bignum(){ 

    for(int i=0;i<SIZE;i++) 

     no[i]=0; 

} 


Bignum (string _no){ 

    for(int i=0;i<SIZE;i++) 

     no[i]=0; 

    int index=SIZE-1; 

    for(int i=_no.length()-1;i>=0;i--,index--){ 

     no[index]=_no[i]-'0'; 

    } 

} 


void print(){ 

    int start=0; 

    for(int i=0;i<SIZE;i++) 

    if(no[i]!=0){ 

     start=i; 

     break;  // find the first non zero digit. store the index in start. 

    } 

    for(int i=start;i<SIZE;i++) // print the number starting from start till the end of array. 

     cout<<no[i]; 

    cout<<endl; 

    return; 

} 
}; 


int main(){ 

Bignum n1("100122354123451234516326245372363523632123458913760187501287519875019671647109857108740138475018937460298374610938765410938457109384571039846"); 
Bignum n2("92759375839475239085472390845783940752398636109570251809571085701287505712857018570198713984570329867103986475103984765109384675109386713984751098570932847510938247510398475130984571093846571394675137846510874510847513049875610384750183274501978365109387460374651873496710394867103984761098347609138746297561762234873519257610"); 

Bignum n3 = n1*n2; 
n3.print(); 

return 0; 

    } 

,你可以看到,它的乘2個巨大的整數:) ...(最多700位)

1

嘗試這種情況:

//------------DEVELOPED BY:Ighit F4YSAL------------- 
#include<iostream> 
#include<string> 
#include<sstream> 


#define BIG 250 //MAX length input 

using namespace std; 

int main(){ 
    int DUC[BIG][BIG*2+1]={0},n0[BIG],n1[BIG],i,t,h,carry=0,res; 
    string _n0,_n1; 
    while(1){ 
    //-----------------------------------get data------------------------------------------ 
    cout<<"n0="; 
    cin>>_n0; 
    cout<<"n1="; 
    cin>>_n1; 
    //--------------------string to int[]---------------------------------------- 
    for(i=_n0.length()-1,t=0;i>=0,t<=_n0.length()-1;i--,t++){ 
    n0[i]=_n0[t]-'0'; 
    } 
    i=0; 
    for(i=_n1.length()-1,t=0;i>=0,t<=_n1.length()-1;i--,t++){ 
    n1[i]=_n1[t]-'0'; 
    } 
    i=0;t=0; 
    //--------------------------produce lines of multiplication---------------- 
    for(i=0;i<=_n1.length()-1;i++){ 
     for(t=0;t<=_n0.length()-1;t++){ 
      res=((n1[i]*n0[t])+carry); 
      carry=(res/10); 
      DUC[i][t+i]=res%10; 
     } 
     DUC[i][t+i]=carry; 
     carry=0; 
    } 
    i=0;t=0;res=0;carry=0; 
    //-----------------------------add the lines------------------------------- 
    for(i=0;i<=_n0.length()*2-1;i++){ 
     for(t=0;t<=_n1.length()-1;t++){ 
     DUC[BIG-1][i]+=DUC[t][i]; 
      //cout<<DUC[t][i]<<"-"; 
     } 
     res=((DUC[BIG-1][i])+carry); 
     carry=res/10; 
     DUC[BIG-1][i]=res%10; 
     //cout<<" ="<<DUC[BIG-1][i]<<endl; 
    } 
    i=0;t=0; 
    //------------------------print the result------------------------------------ 
    cout<<"n1*n0="; 
    for(i=_n0.length()*2-1;i>=0;i--){ 
     if((DUC[BIG-1][i]==0) and (t==0)){}else{cout<<DUC[BIG-1][i];t++;} 
     //cout<<DUC[BIG-1][i]; 
    } 
    //-------------------------clear all------------------------------------- 
    for(i=0;i<=BIG-1;i++){ 
     for(t=0;t<=BIG*2;t++){ 
     DUC[i][t]=0; 
    } 
    n0[i]=0;n1[i]=0; 
    } 
    //--------------------------do it again------------------------------------- 
    cout<<"\n------------------------------------------------\n\n"; 
    } 
    return 0; 
}