淡淡会遗忘 2022-02-22 21:01 采纳率: 90.9%
浏览 261
已结题

求1到100之间所有整数的乘积的结果

像下面这种写法,sum的结果最终会严重超出int的范围,得不到想要的数据,求一个能显示正确结果的程序方法

#include <stdio.h>    
int main()
{
    
    int i=0;
    int sum=1;
    for(i=1;i<=100;i++)
    {
        sum=sum*i;
    }
    printf("%d",sum);
    return 0;
 } 
  • 写回答

3条回答 默认 最新

  • _GX_ 2022-02-22 21:11
    关注

    你需要实现一个任意精度整数乘法,下面是一个C++实现的例子,你可以参考一下。

    #include <iostream>
    #include <string>
    #include <iterator>
    #include <vector>
    
    class BigInt
    {
    public:
        BigInt(unsigned int n = 0)
        {
            if (n == 0)
                _data.push_back('0');
            else
            {
                while (n)
                {
                    _data.push_back(n % 10 + '0');
                    n /= 10;
                }
            }
        }
    
        BigInt(const std::string &s) : _data(s.rbegin(), s.rend()) {}
    
        BigInt &operator+=(const BigInt &other);
        BigInt &operator*=(const BigInt &other);
    
        BigInt operator+(const BigInt &other) const
        {
            BigInt t = *this;
            t += other;
            return t;
        }
    
        BigInt operator*(const BigInt &other) const
        {
            BigInt t = *this;
            t *= other;
            return t;
        }
    
    private:
        std::string _data;
    
        friend std::istream &operator>>(std::istream &is, BigInt &a);
        friend std::ostream &operator<<(std::ostream &os, const BigInt &a);
    };
    
    BigInt &BigInt::operator+=(const BigInt &other)
    {
        int carry = 0;
        std::string::iterator itr1 = _data.begin();
        std::string::const_iterator itr2 = other._data.begin();
        while (itr1 != _data.end() && itr2 != other._data.end())
        {
            int a = *itr1 - '0';
            int b = *itr2 - '0';
            int c = a + b + carry;
            carry = c / 10;
            c %= 10;
            *itr1 = c + '0';
            ++itr1;
            ++itr2;
        }
        while (itr1 != _data.end())
        {
            int a = *itr1 - '0' + carry;
            carry = a / 10;
            a %= 10;
            *itr1 = a + '0';
            ++itr1;
        }
        while (itr2 != other._data.end())
        {
            int a = *itr2 - '0' + carry;
            carry = a / 10;
            a %= 10;
            _data.push_back(a + '0');
            ++itr2;
        }
        if (carry > 0)
            _data.push_back(carry + '0');
        return *this;
    }
    
    BigInt &BigInt::operator*=(const BigInt &other)
    {
        BigInt r;
        for (std::size_t i = 0; i < other._data.size(); i++)
        {
            int x = other._data[i] - '0';
            int carry = 0;
            BigInt t = *this;
            for (std::string::iterator itr = t._data.begin(); itr != t._data.end(); ++itr)
            {
                int y = *itr - '0';
                int a = x * y + carry;
                *itr = a % 10 + '0';
                carry = a / 10;
            }
            if (carry > 0)
                t._data.push_back(carry + '0');
            if (i > 0)
            {
                std::string zeros(i, '0');
                t._data.insert(t._data.begin(), zeros.begin(), zeros.end());
            }
            r += t;
        }
        *this = r;
        return *this;
    }
    
    std::istream &operator>>(std::istream &is, BigInt &a)
    {
        std::string s;
        is >> s;
        a = s;
        return is;
    }
    
    std::ostream &operator<<(std::ostream &os, const BigInt &a)
    {
        std::copy(a._data.rbegin(), a._data.rend(), std::ostream_iterator<char>(os));
        return os;
    }
    
    int main()
    {
        BigInt r = 1;
        for (int i = 1; i <= 100; i++)
            r *= i;
        std::cout << r << std::endl;
        return 0;
    }
    
    $ g++ -Wall main.cpp
    $ ./a.out
    93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月22日
  • 已采纳回答 2月22日
  • 创建了问题 2月22日

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作