不精确的C++阶乘程序

Aks*_*hay 3 c++ arrays biginteger factorial

我编写了以下教程的实现:LINK

基本上,由于C/C++没有BIG Integer,我们将阶乘十进制值存储在一个数组中.这相当于编写一个乘法,用于执行在学校教授孩子的乘法.

问题:对于高达17的值,它可以正常工作!之后(18!,19!,...)它不会输出正确的值.

#include <iostream>
using namespace std;

int main(){
    int fact[1000]={1};
    int n; scanf("%d", &n); //n are the number of factorials we will calculate
    while(n--){
        int number; scanf("%d", &number);   //scan the number
        if(number == 0) printf("%d", 1);
        int flag = number;
        int index = 0, length = 0;
        //following lines we find the length of the entered number
        while(flag!=0){
            fact[index] = flag%10;
            flag /= 10;
            index++; length++;
        }
        //following lines are the multiplication code
        while(number>1){
            index = 0;
            int temp = 0;
            number--;
            for(index = 0; index<length; index++){
                int x = (fact[index] * number) + temp;
                fact[index] = x%10;
                temp = x/10;
            }
            //here we append the carry over left from multiplication
            while(temp){
                fact[index] = temp%10;
                temp /= 10;
                length++;
            }
        }
        //print the array from most to least significant digit
        for(int i = length-1; i>=0; i--){
            printf("%d", fact[i]);
        }
        printf("\n");
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

pax*_*blo 15

首先,您需要非常小心:

long long int x = (fact[index] * number) + temp;
Run Code Online (Sandbox Code Playgroud)

因为fact[],number并且temp都是int类型,则计算将被完成作为int和仅扩大至long long放置值插入时x.

你会更好:

long long x = fact[index];
x *= number;
x += temp;
Run Code Online (Sandbox Code Playgroud)

这样,它就变得long long足够早,可以用这种类型完成计算.


但是,这实际上并没有解决您的问题,所以让我们稍微修改您的代码以查看问题所在:

#include <iostream>
using namespace std;

int main(){
    int fact[1000]={1};
    int n = 18, numberx = 0;
    while(n-- > 0){
        int number = ++numberx;
        if(number == 0) { printf("%d", 1); continue; }
        int flag = number;
        int index = 0, length = 0;
        //following lines we find the length of the entered number
        while(flag!=0){
            fact[index] = flag%10;
            flag /= 10;
            index++; length++;
        }
        //following lines are the multiplication code
        while(number>1){
            index = 0;
            int temp = 0;
            number--;
            for(index = 0; index<length; index++){
                long long int x = fact[index];
                x *= number;
                x += temp;
                fact[index] = x%10;
                temp = x/10;
            }
            //here we append the carry over left from multiplication
            while(temp){
                fact[index] = temp%10;
                temp /= 10;
                length++;
            }
        }
        //print the array from most to least significant digit
        printf("%d! = ", number);
        for(int i = length-1; i>=0; i--){
            printf("%d ", fact[i]);
        }
        printf("\n");
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

运行这个给你:

1! = 1
2! = 2
3! = 6
4! = 2 4
5! = 1 2 0
6! = 7 2 0
7! = 5 0 4 0
8! = 4 0 3 2 0
9! = 3 6 2 8 8 0
10! = 3 6 2 8 8 0 0
11! = 3 9 9 1 6 8 0 0
12! = 4 7 9 0 0 1 6 0 0
13! = 6 2 2 7 0 2 0 8 0 0
14! = 8 7 1 7 8 2 9 1 2 0 0
15! = 1 3 0 7 6 7 4 3 6 8 0 0 0
16! = 2 0 9 2 2 7 8 9 8 8 8 0 0 0
17! = 3 5 5 6 8 7 4 2 8 0 9 6 0 0 0
18! = 1 9 9 1 0 4 7 1 7 3 8 5 7 2 8 0 0 0
Run Code Online (Sandbox Code Playgroud)

也就是说,当你说到18岁之前好吧!如果失败了.而且,事实上,你可以看到17之间的比例!和18!是大约500而不是18,所以这是我们应该看的地方.

让我们首先从 17 开始剥离无关的东西!这可以通过更改几个起始值来完成:

int n = 2, numberx = 16;
Run Code Online (Sandbox Code Playgroud)

这给了:

17! = 3 5 5 6 8 7 4 2 8 0 9 6 0 0 0
18! = 1 9 9 1 0 4 7 1 7 3 8 5 7 2 8 0 0 0
Run Code Online (Sandbox Code Playgroud)

然后我们可以添加调试代码来查看正在发生的事情,并在此过程中输出临时结果.主循环可以变为:

while(number>1){
    index = 0;
    int temp = 0;
    number--;
    if (numberx > 17) printf("\n");
    for(index = 0; index<length; index++){
        if (numberx > 17) printf("index %d fact[] %d number %d temp %d", index, fact[index], number, temp);
        long long int x = fact[index];
        x *= number;
        x += temp;
        fact[index] = x%10;
        temp = x/10;
        if (numberx > 17) printf(" -> fact[] %d temp %d\n", fact[index], temp);
    }
    //here we append the carry over left from multiplication
    while(temp){
        fact[index] = temp%10;
        temp /= 10;
        length++;
    }
    if (numberx > 17) {
        printf("temp: ");
        for(int i = length-1; i>=0; i--){
            printf("%d ", fact[i]);
        }
        printf("\n");
    }
}
Run Code Online (Sandbox Code Playgroud)

这显示了*确切地出现问题的地方(//由我添加的位):

17! = 3 5 5 6 8 7 4 2 8 0 9 6 0 0 0

index 0 fact[] 8 number 17 temp 0 -> fact[] 6 temp 13
index 1 fact[] 1 number 17 temp 13 -> fact[] 0 temp 3
temp: 3 0 6 // okay: 18 * 17 = 306

index 0 fact[] 6 number 16 temp 0 -> fact[] 6 temp 9
index 1 fact[] 0 number 16 temp 9 -> fact[] 9 temp 0
index 2 fact[] 3 number 16 temp 0 -> fact[] 8 temp 4
temp: 4 8 9 6 // okay 306 * 16 = 4896

index 0 fact[] 6 number 15 temp 0 -> fact[] 0 temp 9
index 1 fact[] 9 number 15 temp 9 -> fact[] 4 temp 14
index 2 fact[] 8 number 15 temp 14 -> fact[] 4 temp 13
index 3 fact[] 4 number 15 temp 13 -> fact[] 3 temp 7
temp: 7 3 4 4 0 // okay 4896 * 15 = 73440

index 0 fact[] 0 number 14 temp 0 -> fact[] 0 temp 0
index 1 fact[] 4 number 14 temp 0 -> fact[] 6 temp 5
index 2 fact[] 4 number 14 temp 5 -> fact[] 1 temp 6
index 3 fact[] 3 number 14 temp 6 -> fact[] 8 temp 4
index 4 fact[] 7 number 14 temp 4 -> fact[] 2 temp 10
temp: 8 1 2 8 1 6 0 // no good: 73440 * 14 = 10128160 !!!
      1 0 2 8 1 6 0 // is what it should be
Run Code Online (Sandbox Code Playgroud)

通过一些思考,似乎是乘法的最终"携带"大于9的意义,这意味着它几乎可以肯定在处理代码中:

while(temp){
    fact[index] = temp%10;
    temp /= 10;
    length++;
}
Run Code Online (Sandbox Code Playgroud)

有关思考(并将其与改变其他代码index和length一起),可以很明显-即使你增加长度的数组,你不增加指标.这意味着,对于十个或更多的最终进位,后续进位将不会填充正确的索引,它每次只会覆盖相同的索引.

这可以在这里看到:

temp: 8 1 2 8 1 6 0 // no good: 73440 * 14 = 10128160 !!!
      1 0 2 8 1 6 0 // is what it should be
Run Code Online (Sandbox Code Playgroud)

它将零(10%10)置于第二个位置(增加长度),然后将一个(10/10)放在相同的索引处,留下8之前的任何值.

那么,如果我们也增加index,我们会看到什么(回到较不详细的代码)?

1! = 1
2! = 2
3! = 6
4! = 2 4
5! = 1 2 0
6! = 7 2 0
7! = 5 0 4 0
8! = 4 0 3 2 0
9! = 3 6 2 8 8 0
10! = 3 6 2 8 8 0 0
11! = 3 9 9 1 6 8 0 0
12! = 4 7 9 0 0 1 6 0 0
13! = 6 2 2 7 0 2 0 8 0 0
14! = 8 7 1 7 8 2 9 1 2 0 0
15! = 1 3 0 7 6 7 4 3 6 8 0 0 0
16! = 2 0 9 2 2 7 8 9 8 8 8 0 0 0
17! = 3 5 5 6 8 7 4 2 8 0 9 6 0 0 0
18! = 6 4 0 2 3 7 3 7 0 5 7 2 8 0 0 0
19! = 1 2 1 6 4 5 1 0 0 4 0 8 8 3 2 0 0 0
20! = 2 4 3 2 9 0 2 0 0 8 1 7 6 6 4 0 0 0 0
Run Code Online (Sandbox Code Playgroud)

这解决了您的具体问题,并希望提供一些调试教育:-)