C语言程序,使用3位数字,但不使用5位数字

C语言程序,使用3位数字,但不使用5位数字,c,factorial,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

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;
    }
}
#包括
整数阶乘(intn);
main(){
int引脚[5];
int q=1;
int w=0;
int e=0;
int r=0;
int t=0;
int结果=0;
整数和=0;

对于(q=1;q您的
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;
        }
}
它仅在进行递归调用时返回一个值。基本情况不返回任何内容。未能从函数返回值,然后尝试使用该值调用

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

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

dbush的回答准确地指出了代码不起作用的原因。这是一种替代解决方案,通过不在每一步重新计算每个数字的阶乘来减少程序的计算量。按照您的程序目前的工作方式,它最终会从yo调用约500000个阶乘函数ur嵌套循环,然后从嵌套循环中每次调用平均递归调用函数4次,因此大约有200万次调用
factorial
。添加的数字越多,该数字增长越快,成本也越高。为了避免所有这些重新计算,您可以创建
查找表
它存储数字的阶乘
[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;
   }
}
#包括
#包括
#包括
#包括
无效填充lut(uint32*lut);
内部主(空){
//lut是一个包含数字0-9的阶乘的数组
uint32_t lut[10];
填充lut(lut);

对于(uint8_t q=1;q)可能它只是没有完成练习,坚持认为只有一个5位数字符合“代码”首先,我尝试并成功地编写了适用于3位数字的代码,并给了我145,正如练习中提到的!因此,我只添加了2位数字,我希望它能工作。它不会返回任何结果。任何帮助都会很好。我正在尝试4个小时:/@Hogan,你是什么意思,我的朋友?他可能意味着你有5-deep调用递归函数。您的程序正在做大量的工作,可能无法在您期望的时间范围内完成。时间的增长是非线性的,从3到5移动不需要80%的时间,可能需要10000%的时间。所以它还没有完成。非常感谢你们!不仅解决了这个问题,而且我还获得了宝贵的信息请确认我的语法!谢谢。顺便说一下,电话号码是40585:)@dimcon很高兴我能提供帮助。欢迎[接受此答案]9https://stackoverflow.com/help/accepted-answer)如果你觉得有用的话。当然。谢谢你提醒我。这是我的第一篇帖子。仍在努力熟悉。:)
#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;
   }
}