用C编程,使用3位数但不能使用5位数

问题描述 投票:2回答:2

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;
    }
}
c factorial
2个回答
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);

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;
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.