用C语言编写的程序,可以使用3位数字,但不能使用5位数字

时间:2019-02-25 21:00:19

标签: c factorial

145 = 1! + 4! + 5!的总和。我需要用C编写一个程序,以找到具有此属性的5位数字。

我已经成功地为3位数字编写了代码。我使用相同的代码输入5位数字,但找不到任何数字。

我想为我的解决方案提供帮助,以便让我知道我在哪里错了。

#include <stdio.h>

int factorial(int n);

main() {
    int pin[5];

    int q = 1;
    int w = 0;
    int e = 0;
    int r = 0;
    int t = 0;

    int result = 0;

    int sum = 0;

    for (q = 1; q <= 9; q++) {
        for (w = 0; w <= 9; w++) {
            for (e = 0; e <= 9; e++) {
                for (r = 0; r <= 9; r++) {
                    for (t = 0; t <= 9; t++) {
                        pin[0] = q;
                        pin[1] = w;
                        pin[2] = e;
                        pin[3] = r;
                        pin[4] = t;

                        int factq = factorial(q);
                        int factw = factorial(w);
                        int facte = factorial(e);
                        int factr = factorial(r);                                                                       
                        int factt = factorial(t);

                        sum = factq + factw + facte + factr + factt;                
                        result = 10000 * q + 1000 * w + 100 * e + 10 * r + t * 1;

                        if (sum == result)
                            printf("ok");
                    }
                }
            }
        }
    }
}

int factorial(int n) {
    int y;
    if (n == 1) {
        y = 1;
    } else if (n == 0)
        y = 0;
    else {
        y = n * factorial(n - 1);
        return y;
    }
}

2 个答案:

答案 0 :(得分:4)

您的factorial函数并非在所有情况下都返回值:

int factorial (int n) {
    int y;
    if (n==1) {
        y = 1;
    }
    else 
        if (n==0)
            y = 0;
        else {
            y = n * factorial(n-1);
            return y;
        }
}

仅在进行递归调用时返回一个值。基本情况不返回任何内容。无法从函数返回值,然后尝试使用该值将调用undefined behavior

return语句移到函数底部,以便在所有情况下都可以调用它。此外,0!的值为1,而不是0。

int factorial (int n) {
    int y;
    if (n<=1)
        y = 1;
    else 
        y = n * factorial(n-1);
    return y;
}

此外,当您找到目标值时,您可能想要打印它:

printf("ok: %d\n", result);

答案 1 :(得分:0)

dbush的答案准确地指出了为什么您的代码不起作用。这是一种替代解决方案,可以通过不重新计算每个数字阶乘来减少程序所进行的计算量。程序当前的工作方式是,从嵌套循环中调用析因函数大约有500,000次调用,然后依次为嵌套循环中的每次调用递归地平均调用该函数4次,因此大约有200万次调用factorial。您增加的数字越多,数字增长得越快,获得的价格就越高。为了避免所有这些重新计算,您可以创建一个Look-up table,用于存储数字[0-9]的阶乘,并根据需要进行查找。

您可以提前计算这些值,并使用这些值初始化LUT,但是如果您想让它们由程序计算,则因为这是一个编程任务,您无法执行此步骤,因此填充LUT仍然很琐碎。

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>

void populate_lut(uint32_t *lut);

int main(void) {
   // lut is an array holding the factorials of numerals 0-9
   uint32_t lut[10];
   populate_lut(lut);
    for (uint8_t q = 1; q <= 9; q++) {
        for (uint8_t w = 0; w <= 9; w++) {
            for (uint8_t e = 0; e <= 9; e++) {
                for (uint8_t r = 0; r <= 9; r++) {
                    for (uint8_t t = 0; t <= 9; t++) {
                        // now instead of calculating these factorials, just look them up in the look-up table
                        uint32_t sum = lut[q] + lut[w] + lut[e] + lut[r] + lut[t];                
                        uint32_t result = 10000 * q + 1000 * w + 100 * e + 10 * r + t * 1;

                        if (sum == result) {
                            printf("Solution: %" PRIu32 "\n", result);
                        }
                    }
                }
            }
        }
    }
}

// populate your lookup table with the factorials of digits 0-9
void populate_lut(uint32_t *lut) {
   lut[0] = 1;
   lut[1] = 1;
   for(uint8_t i = 2; i < 10; ++i) {
      lut[i] = lut[i-1] * i;
   }
}