阶乘函数仅在C ++中返回输入的答案?

时间:2018-08-01 23:45:59

标签: c++ factorial

我必须创建一个程序来计算任何数字的阶乘,问题是如果我输入20以上的任何数字,它将返回该数字。在我的其他if语句中可能会导致这种情况吗?有没有更好的方法来解决这个问题? (此函数在main中调用,并且在num <= 20时有效)

void factorial() {

//User input for number
long long num;
std::cout << "Input any positive integer to find its factorial: ";
std::cin >> num;

unsigned long long numFact = 1;
if (num <= 20) {

    while (num > 0) {
        numFact = numFact * num;
        num = num - 1;
    }
    std::cout << numFact;
}

else if (num > 20) {

    std::vector<int> multFactorial;

    //stores num as seperate elements in vector multFactorial
    while (num > 0) {
        int remain = num % 10;
        num = num / 10;
        multFactorial.insert(multFactorial.begin(), remain);
    }
    std::vector<int> answer;
    std::vector<int> answerFinal;

    //Manually multiplies elements in multFactorial
    //Then adds new vectors created by multiplying to get final answer
    //Repeats until factorial is solved
    //Ex: 21 * 20; 0 * 1 and 0 * 2 stored as {0 , 0}
    //2*1 and 2*2 stored as {4, 2, 0}
    //Vectors will be addes to get {4, 2, 0} and then that will be multiplied 
by 19 until num = 1
    while (num > 1) {

        for (int i = multFactorial.size() - 1; i >= 0; i--) {
            int remain1 = ((num - 1) % 10) * multFactorial[i];
            answer.insert(answer.begin(), remain1);
            int remain2 = (((num - 1) / 10) * multFactorial[i]);
            answerFinal.insert(answerFinal.begin(), remain2);
        }

        answerFinal.insert(answerFinal.begin(), 0);

        //Adds vectors to get final value seperate as digits
        for (int i = multFactorial.size() - 1; i >= 0; i--) {
            multFactorial[i] = answer[i] + answerFinal[i];
        }
        num = num - 1;

    }
    //Prints what should be the factorial of the number input
    for (size_t i = 0; i < multFactorial.size(); i++) {
        std::cout << multFactorial[i];
    }
}
}

1 个答案:

答案 0 :(得分:1)

大量工厂生产大量产品。通过将结果放入任意长度的字符串中,可以在C,C ++等语言中容纳该变量。

这是一种算法-与您的算法类似。

https://www.geeksforgeeks.org/factorial-large-number/

最好的建议是对照此检查您的代码。 如果您有一个调试器,请逐一调试代码。 如果没有,请打印出中间结果,并与预期结果进行比较。

编辑:根据评论注释,上面引用处的代码与下面类似,以防万一将来链接中断。

// C++ program to compute factorial of big numbers
#include<iostream>
using namespace std;

// Maximum number of digits in output
#define MAX 100   // change to whatever value you need

int multiply(int x, int res[], int res_size);

// Calculate factorial of large number
void factorial(int n)
{
    int res[MAX];

    // Initialize result
    res[0] = 1;
    int res_size = 1;

    // Apply factorial formula
    for (int x=2; x<=n; x++)
        res_size = multiply(x, res, res_size);

    // print out the result 
    cout << "Factorial is \n";
    for (int i=res_size-1; i>=0; i--)
        cout << res[i];
}

// Multiplies x with the number represented by res[].
// res_size is size of res[] or number of digits in the 
// number represented by res[]. 

int multiply(int x, int res[], int res_size)
{
    int carry = 0;  // Initialize carry

    // One by one multiply n with individual digits of res[]
    for (int i=0; i<res_size; i++)
    {
        int prod = res[i] * x + carry;

        // Store last digit of 'prod' in res[]  
        res[i] = prod % 10;  

        // Put rest in carry
        carry  = prod/10;    
    }

    // Put carry in res and increase result size
    while (carry)
    {
        res[res_size] = carry%10;
        carry = carry/10;
        res_size++;
    }
    return res_size;
}

// Main program
int main()
{
    //put code here to read a number

    factorial(50); // take 50 for example
    return 0;
}