较大整数乘积:算法题

// jobInterView.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <iostream>

#include <vector>
#include <string>
#include <algorithm>

#include <ctime>



/*
 * 1. num1 和 num2 长度 小于 110 < 110
 * 2. num1 和 num2 只包含 数字 0 - 9
 * 3. num1 和 num2 均不以0开头,除非数字 0.
 * 4. 不能使用大数据类型。只能使用整数,整型。
 * 5. num1 与 num2 均 >= 0;
 */
/*
输入:
0 1
99999999999999999999 99999999999999999999
输出:
0
9999999999999999999800000000000000000001

        9 9 9
      *   9 9 // 3:   3  sum;
     ---------    8 1 ---> 1,   carry ... 8;
      8 9 9 1
    8 9 9 1 0
  8 9 9 1 0 0
-------------------
 10 9 7 9 0 1
      9 8 7
          9
    --------

       8 1
     8 1

*/
static std::string addTwoStringNumber(std::string const& a, std::string const& b);

static std::string multiStringByChar(std::string const & a, char b);

std::string multiOfTwoBigInt(std::string const & a, std::string const & b);

int main()
{
    std::cout << multiOfTwoBigInt("99999999999999999999", "99999999999999999999")
        << "\n9999999999999999999800000000000000000001\n";
    std::cout << "----------------------------------------------\n";
    std::cout << multiOfTwoBigInt("1402", "103") << "\n" << 1402 * 103 << std::endl;
    return 0;
}

std::string multiOfTwoBigInt(std::string const & a, std::string const & b)
{
    if (a.size() < b.size())
    {
        return multiOfTwoBigInt(b, a);
    }
    std::string res = "", cursor = "";
    for (int i = 0; i < b.size(); i++)
    {
        if (b[i] == '0')
        {
            if (!res.empty())
            {
                res.push_back('0');
            }
            continue;
        }
        if (i == 0)
        {
            res = multiStringByChar(a, b[0]);
            continue;
        }
        cursor = multiStringByChar(a, b[i]);
        res.push_back('0');
        res = addTwoStringNumber(res, cursor);
    }
    return res;
}

std::string addTwoStringNumber(std::string const& a, std::string const& b)
{
    std::string res = "";
    int i = a.size() - 1, j = b.size() - 1;
    int m = 0, n = 0, c = 0, r = 0;
    for (; i >= 0 && j >= 0; i--, j--)
    {
        m = a[i] - '0';
        n = b[j] - '0';
        r = m + n + c;
        if (r >= 10)
        {
            c = 1;
        }
        else
        {
            c = 0;
        }
        r %= 10;
        res.push_back((char)(r + '0'));
    }
    while (i >= 0)
    {
        m = a[i--] - '0';
        r = m + c;
        if (r >= 10)
        {
            c = 1;
        }
        else
        {
            c = 0;
        }
        r %= 10;
        res.push_back((char)(r + '0'));
    }
    while (j >= 0)
    {
        n = b[j--] - '0';
        r = n + c;
        if (r >= 10)
        {
            c = 1;
        }
        else
        {
            c = 0;
        }
        r %= 10;
        res.push_back((char)(r + '0'));
    }
    if (c > 0)
    {
        res.push_back((char)('1'));
    }
    std::reverse(res.begin(), res.end());
    return res;
}

std::string multiStringByChar(std::string const & a, char b)
{
    int m = b - '0', c = 0;
    std::string res = "";
    for (int i = a.size() - 1; i >= 0; i--)
    {
        int n = a[i] - '0';
        int r = m * n + c;
        c = r / 10;
        r %= 10;
        res.push_back(char(r + '0'));
    }
    if (c > 0)
    {
        res.push_back(char(c + '0'));
    }
    std::reverse(res.begin(), res.end());
    return res;
}

 

发布了13 篇原创文章 · 获赞 0 · 访问量 346
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览