加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C中功率非常大

发布时间:2020-12-16 09:49:35 所属栏目:百科 来源:网络整理
导读:有人可以用一点时间,他可以向我解释一下如何强化非常大的数字?我不是在这里谈论一个现成的解决方案,以及如何实现算法的唯一解释.理想情况下,它基于类std :: string. @编辑 我读了一些关于shift位的内容,但是示例只是以列表的形式出现,我想要解释它是如何工
有人可以用一点时间,他可以向我解释一下如何强化非常大的数字?我不是在这里谈论一个现成的解决方案,以及如何实现算法的唯一解释.理想情况下,它基于类std :: string.

@编辑

我读了一些关于shift位的内容,但是示例只是以列表的形式出现,我想要解释它是如何工作的.

解决方法

这是我在需要时快速写的东西(不记得何时).它是:

>越野车;
>不完整;
>任意使用每个数组元素3位数,而它可以使用更多;
>可以明显改进(欢迎任何一种评论^^).

但是,我希望这会有所帮助.

typedef long long int lli;

class BigInt
{
public: // Methods
    BigInt(lli s) : m_nbElements(100)
    {
        m_number.resize(m_nbElements);

        for (lli i=0; i < m_nbElements; ++i)
        {
            m_number[i] = s%1000;
            s /= 1000;
        }
    }

    BigInt(const std::string &str) : m_nbElements(100)
    {
        m_number.resize(m_nbElements);

        size_t sizeStr = str.size();
        int i = str.size() - 1;
        int thousands = 0;
        for (; i >= 2; i -= 3,++thousands)
        {
            std::string subStr = str.substr(i-2,3);
            unsigned int value;
            std::istringstream(subStr) >> value;
            m_number[thousands] = value;
        }

        // Handle the "first" 1 or 2 digits
        if (i >= 0)
        {
            std::string subStr = str.substr(0,i+1);
            unsigned int value;
            std::istringstream(subStr) >> value;
            m_number[thousands] = value;
        }
    }

    BigInt operator*(lli s)
    {
        lli temp,remainder = 0;
        for (lli i=0; i < m_nbElements; ++i)
        {
            temp = m_number[i] * s + remainder;
            m_number[i] = temp % 1000;
            remainder = temp / 1000;
        }

        return (*this);
    }

    BigInt operator/(lli s)
    {
        lli temp,remainder = 0;
        for (int i=m_nbElements-1; i >= 0; --i)
        {
            temp = (m_number[i] + remainder) / s;
            remainder = (m_number[i] % s)*1000;
            m_number[i] = temp;
        }

        return (*this);
    }

    BigInt operator-(BigInt s)
    {
        lli temp;
        for (unsigned int i=0; i < m_nbElements; ++i)
        {
            temp = m_number[i] - s.m_number[i];
            if (temp < 0)
            {
                --m_number[i+1];
                temp += 1000;
            }
            m_number[i] = temp;
        }

        return (*this);
    }

    BigInt operator+(BigInt s)
    {
        lli temp,remainder = 0;
        for (lli i=0; i < m_nbElements; ++i)
        {
            temp = m_number[i] + s.m_number[i] + remainder;
            m_number[i] = temp % 1000;
            remainder = temp / 1000;
        }

        return (*this);
    }

    std::string ToString()
    {
        std::string result = "";
        bool significantDigitsFound = false;
        for (int i=m_nbElements-1; i >= 0 ; --i)
        {
            if (!significantDigitsFound)
            {
                if (m_number[i] > 0)
                {
                    std::ostringstream ss;
                    ss << m_number[i];
                    result = ss.str();

                    significantDigitsFound = true;
                }
            }
            else
            {
                std::ostringstream ss;
                ss << std::setw(3) << std::setfill( '0' ) << m_number[i];
                result += ss.str();
            }
        }

        if (result == "")
        {
            result = "0";
        }

        return result;
    }

private: // Attributes
    int m_nbElements;
    std::vector<lli> m_number;
};

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读