zoukankan      html  css  js  c++  java
  • 大数加乘

     1 #include <bits/stdc++.h> //大数相加
     2 using namespace std;
     3 
     4 int main()
     5 {
     6     string a, b, res;
     7     cin >> a >> b;
     8     int i, k, inc, lena, lenb, temp;
     9     lena = a.length(); lenb = b.length();
    10     for (i = lena - 1, k = lenb - 1, inc = 0; i >= 0 || k >= 0; --i, --k)
    11     {
    12         temp = inc;
    13         if (i >= 0)
    14         {
    15         temp += a[i] - '0';
    16         }
    17         if (k >= 0)
    18         {
    19             temp += b[k] - '0';
    20         }
    21         res += temp % 10 + '0';
    22         inc = temp / 10;
    23     }
    24     if (inc)
    25     {
    26     res += inc + '0';
    27     }
    28     reverse(begin(res), end(res));
    29     cout << res << endl;
    30     return 0;
    31 }
    #include<bits/stdc++.h> //大数相乘
    using namespace std;
    
    string strAdd(string a, string b, int shift)
    {
        int i, j, inc, temp;
        string res;
        while (shift-- > 0)
        {
            b = '0' + b;
        }
    
        temp = inc = 0;
        int lena = a.length();
        int lenb = b.length();
    
        for (i = j = 0; i < lena || j < lenb; )
        {
    	temp = inc;
    	if (i < lena)
    	{
            	temp += a[i] - '0';
    		++i;
    	}
    	if (j < lenb)
    	{
    		temp += b[j] - '0';
    		++j;
    	}      
    	inc = temp / 10;
    	res += temp % 10 + '0';
        }
        if (inc)
            res += inc + '0';
        return res;
    }
    
    string strMulti(string a, string b)
    {
        string res = "0";
        int i, j, lena, lenb, nTemp, inc;
        lena = a.length(), lenb = b.length();
        
        reverse(begin(a), end(a));
        reverse(begin(b), end(b));
        
        for (i = 0; i < lena; ++i)
        {
           	nTemp = inc = 0;
            string strTemp = "";
            for (j = 0; j < lenb; ++j)
            {
                nTemp = (a[i] - '0') * (b[j] - '0') + inc;
                inc = nTemp / 10;
                strTemp += nTemp % 10 + '0';
            }
    	if (inc > 0)
    	{
    		strTemp += inc + '0';
    	}
           res = strAdd(res, strTemp, i);
        }
        reverse(begin(res), end(res));
        bool flag = true;
        string finalRes;
        for (char c : res)
        {
    	if (c == '0' && flag)
    	{
    		continue;
    	}
    	else 
    	{
    		flag = false;
    	}
    	finalRes += c;
        }
        return finalRes;
    }
    
    int main()
    {
        string a, b;
        cin >> a >> b;
        cout << strMulti(a, b) << endl;
        return 0;
    }
    

      

  • 相关阅读:
    放假归来
    用ObjectSpaces重建IBuySpy的数据访问层
    在SPS中加入自定义WebService
    AnnouncementOSD.xml
    Delphi8 is out !
    ASP.NET PreCompilation and KeepAlive
    ScottGu回答了Whidbey发布的时间问题
    DiskBased Caching in Whidbey, Longhorn…
    AnnouncementRSD.xml
    忙着满足客户的需求...
  • 原文地址:https://www.cnblogs.com/deepspace/p/10336037.html
Copyright © 2011-2022 走看看