洛谷题P1009:使用高精度求阶乘。为什么for循环发生内存溢出了?该如何修改?要怎样避免?

img

img


 
#include<bits/stdc++.h>
 
using namespace std;
 
int n, c[250],sum[250];
 
void pluss(int n,int b[])
{
    if (n == 1)  return ;
    int  a[250] = { -1 };
    for (int i = 0; n; i++)
    {
        a[i] = (n % 10);
        n /= 10;
    }
    
    for (int i = 1; a[i] != -1; i++)
    {
        for (int j = 1; b[j] != -1; j++)
        {
            c[i + j - 1] += a[i] * b[j];
            c[i + j] += (c[i + j - 1] / 10);
            c[i + j] %= 10;
        }
    }
 
    pluss(n - 1, c);  
}
 
int main()
{
    cin >> n;
    
    while (n)
    {
        int b[250] = { -1 };
        memset(b, -1, sizeof(b));
        b[1] = 1;
        pluss(n, b);
 
        for (int i = 1; i < 250; i++)
        {
            sum[i] += c[i];
            sum[i + 1] += (sum[i] / 10);
            sum[i] = (sum[i] % 10);
        }
 
        memset(c, 0, sizeof(c));
    }
 
    for (int i = 249; i > 1; i--)
    {
        if (sum[i] != 0)
        {
            for (int j = i; j > 1; j--)
            {
                cout << sum[j];
            }
            cout << '\n';
            return 0;
        }
    }
 
}

数组越界异常了。

你的a数组应该需要初始化吧?
否则的话19行的循环几乎必定越界的

把数组开大一些试试。比如c[250]开c[10000];
以上仅供参考,如有任何疑问,可以评论回复,看到即回,希望对题主有所帮助!

#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;
    auto itr1 = _data.begin();
    auto 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 (auto 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()
{
    int n;
    std::cin >> n;
    BigInt sum, f = 1;
    for (int i = 1; i <= n; i++)
    {
        f *= i;
        sum += f;
    }
    std::cout << sum << std::endl;
    return 0;
}
$ g++ -Wall main.cpp
$ ./a.out
3
9
$ ./a.out
50
31035053229546199656252032972759319953190362094566672920420940313

#include <iostream>
#include <cstring> 
using namespace std;
int num[10000] = {0}, size;
int main(){
    unsigned int n; // 算n!
    cin >> n; // n要大于等于0 
    num[0] = 1;
    size = 1;
    for (int i = 2; i <= n; ++i) {
        int flag = 0;
        for (int j = 0; j < size; ++j) {
            num[j] *= i;
            num[j] += flag % 10; // 要flag的后一位 
            flag /= 10; 
            flag += num[j] / 10;
            num[j] = num[j] % 10; 
        }
        while(flag) {
            num[size++] = flag % 10;
            flag /= 10;
        }
    }
    for (int i = size-1;i >= 0;--i) {
        cout << num[i];
    }
    cout << endl;
}