不正确的C ++阶乘程序

时间:2018-06-25 11:13:10

标签: c++ arrays biginteger factorial

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

基本上,由于C / C ++没有BIG整数,因此我们将阶乘十进制值存储在数组中。这等效于编写一个乘法,该乘法执行孩子在学校受教的乘法。

问题:它适用于高达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;
}

1 个答案:

答案 0 :(得分:15)

一开始,您需要非常小心:

long long int x = (fact[index] * number) + temp;

由于fact[]numbertemp都是int类型,因此计算将作为int完成 ,并且仅在将值放入long long时才扩展为x

您最好选择:

long long x = fact[index];
x *= number;
x += temp;

这样,它已经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;
}

运行此操作会给您:

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

,直到您指出直到18!为止,否则失败。而且,实际上,您可以看到17之间的比例!和18!大约是500,而不是18,所以我们应该看看。

首先让我们从17岁开始 删除多余的内容。只需更改几个起始值即可完成此操作:

int n = 2, numberx = 16;

这给出了:

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

然后,我们可以添加调试代码以查看发生了什么,并一路输出临时结果。主循环可以变成:

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");
    }
}

这向您显示*哪里开始出错了(我添加了//位):

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

稍微想一想,似乎乘法运算的最终“进位”大于九,这意味着在处理该代码的过程中几乎可以肯定:

while(temp){
    fact[index] = temp%10;
    temp /= 10;
    length++;
}

考虑到这一点(并将其与同时更改indexlength的其他代码进行比较),这很明显-即使您增加了数组的 length ,您没有增加索引。这意味着,对于最后的进位为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

它将零(10%10)放置在第二个位置(增加长度),然后将那个(10/10)放置在 same 索引处,保留{{ 1}},无论以前有什么价值。

因此,如果我们也增加8,我们会看到什么(回到不太冗长的代码)?

index

这可以解决您的特定问题,并希望能提供一些有关调试的知识:-)