大整數相乘問題

來源:互聯網
上載者:User

    昨天在CSDN上看到一個大整數相乘的面試題!嘩~~~~好像好久沒練習過了,一起也不知怎麼下手!!看看樓主的答案!!!但是看不出什麼~~~~~唉~~~這段時間都沒什麼練習啦~~~~~感覺應該不難呀!!~~~~於是這個問題一直在我腦海裡打轉~~~~~~把它做出來!!(這段時間都在忙複習,還要趕課程設計,這樣的小演算法題突然覺得很生外了哈~~~~)

以下是我的實現方案:

#include<iostream>

using namespace std;

static int a[10] = {9,3,2,6,4,3};
static int b[10] = {3,7,1,5,5,6};
static int c[20];
//a:乘數1 na;a的位元
//b:乘數2 nb:b的位元
//c:儲存積
int mul(int na, int nb)
{
 int i,j,k,l;
 

 for(i=0;i<na;i++)
 {
  k=i;
  for(j=0;j<nb;j++)
  {
   c[k] = c[k]+b[j]*a[i];
   l = k;
   while(c[l]>=10)
   {
    c[l+1] += (c[l]/10);
    c[l] %= 10;
    l++;
   }  
   k++;
  }

 }
 
 return k;
}

int main()
{
 mul(6,6);

 
 int i=20;
 while( c[i--] ==0 );
 i++;
 for(;i>=0;i--)
  cout<<c[i];
 cout<<endl;

 return 0;
}

----------------------------------------

由於只是想試試做出來,所以沒什麼再深入下去了~~~~試了幾個數可以通過!!不過就要自已指定輸入值和位元!:)

還要趕著英語六級呀~~~~~~慘!!!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.