首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C语言程序,使用3位数,但不使用5位数

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

Stack Overflow用户
提问于 2019-02-26 05:00:19
回答 2查看 84关注 0票数 2

145 = 1! + 4! + 5!的总和。我需要用C语言写一个程序,找出有这个属性的5位数。

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

我想用我的解决方案来帮助我,以便让我看到我哪里错了。

代码语言:javascript
复制
#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;
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-26 05:09:51

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

代码语言:javascript
复制
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。

代码语言:javascript
复制
int factorial (int n) {
    int y;
    if (n<=1)
        y = 1;
    else 
        y = n * factorial(n-1);
    return y;
}

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

代码语言:javascript
复制
printf("ok: %d\n", result);
票数 3
EN

Stack Overflow用户

发布于 2019-02-26 06:47:34

在指出你的代码不能工作的原因上,dbush的回答是准确的。这是一种减少程序计算量的替代解决方案,它不需要每一步都重新计算每个数字的阶乘。按照你的程序目前的工作方式,它结束了大约500,000次从你的嵌套循环中对阶乘函数的调用,然后反过来递归地调用该函数,每次从嵌套循环中调用平均4ish次,所以这大约是对factorial的200万次调用。你添加的数字越多,这个数字增长就越快,成本也就越高。为了避免所有这些重新计算,您可以创建一个存储数字[0-9]阶乘的Look-up table,并根据需要查找它们。

你可以提前计算这些值,并用这些值初始化你的LUT,但是如果假设你想让程序计算它们,因为这是一个编程赋值,你不能省去这样的步骤,填充LUT仍然是相当琐碎的。

代码语言:javascript
复制
#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;
   }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54874582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档