高精度計算(三) /*高精度的乘法運算*/

來源:互聯網
上載者:User

標籤:--   turn   div   char   pre   高精度   strong   span   stream   

 高精度乘單精確度

求 a=a*b。( 0<a<10250,  0<b<108)

輸入:

     第一行:a

     第二行:b

輸出:

  a*b 的值。

範例輸入:

   330

   100

範例輸出:

   33000
 
分析:

     1)a 的每一位都單獨與 b 相乘;

     2)再由低到高位依次處理 a 的進位;

     3)最後處理最高位。

程式實現:

   

 1 //高精度乘單精確度  2 #include <iostream> 3 #include <cstring> 4 using namespace std; 5  6 char s1[300], s2[300]; 7 int a[300]; 8  9 int main()10 {11     int la, b, m;12     cin >> s1 >> b;13     la = strlen(s1);14     for(int i=1;i<=la;i++)15         a[i] = s1[la-i] - 48;16     for(int i=1;i<=la;i++)17         a[i] = a[i] * b;18     for(int i=1;i<=la;i++)19     {20         a[i+1] += a[i] / 10;21         a[i] = a[i] % 10;22     }23     m = a[la+1];24     while(m>0)25     {26         a[++la] = m % 10;27         m = m/10;28     }29     for(int i=la;i>=1;i--)30         cout << a[i];31     32     return 0;33 }34  

 

高精度計算(三) /*高精度的乘法運算*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.