寻找具有常数和的 1 和 2 的可能组合

发布于 2024-10-12 02:56:34 字数 145 浏览 5 评论 0原文

这是用 C 语言完成的任务。你能告诉我如何处理这个问题吗?

火车的长度为n米。它由长度为 1 或 2 米的单独隔间组成。对于给定长度的火车,存在多少种不同的此类车厢组合?编写一个函数 Train (n) 来计算此值。

This is a task to be done in C. Can you tell me how to approach this?

A train has a length of n meters. It is made up of individual compartments of 1 or 2 meters in length. How many different combinations of such compartments exist for a train of a given length? Write a function Train (n) that computes this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

卖梦商人 2024-10-19 02:56:34

从最简单的案例开始,寻找规律。

  1. Train (1) 显然是 1: (1)。
  2. Train (2) 显然是 2:(1 1) 和 (2)。
  3. 训练 (3) 是 3:(1 1 1)、(1 2) 和 (2 1)。前两者可以组合成连接 (1) 和 (1 1) 或 (2)。后者正是Train (2)的组合。因此,Train (3)Train (2) + 1
  4. 火车 (4) 为 5:(1 1 1 1) (1 1 2) (1 2 1) (2 1 1) (2 2)。同样,我们可以将第一个组合为 (1) 与 (1 1 1)、(1 2) 和 (2 1),它们是 Train (3) 的组合。最后是 (2) 与 (1 1) 和 (2) 的结合,它们是 Train (2) 的组合。因此,Train (4)Train (3) + Train (2)。现在,回顾 Train (3),我们看到 + 1Train (1)

现在看来很明显,Train (n) 始终是 Train (n - 1) + Train (n - 2)。这正是斐波那契数列的定义。

现在,让我们看看如何将其转换为 C。

  1. 函数骨架:Train 接受一个整数参数并返回一个整数:

    int 火车 (int n) {
    }
    
  2. 我们得出的定义:

    int 火车 (int n) {
        返回 火车 (n - 1) + 火车 (n - 2);
    }
    
  3. 这将无限递归,因此我们需要在基本情况下停止它。一个基本情况是明确的:Train (1) 是 1:

    int 火车 (int n) {
        如果(n==1){
            返回1;
        } 别的 {
            返回 火车 (n - 1) + 火车 (n - 2);
        }
    }
    
  4. 这仍然不够。想象一下 Train (2) 的作用:它将计算 Train (1) + Train (0)Train (1) 没问题,但是 Train (0) 会计算 Train (-1) + Train (-2),这又是无限递归。因此,我们需要另一个基本情况:Train (2) 是 2。

    int 火车 (int n) {
        如果(n==1){
            返回1;
        } 否则如果 (n == 2) {
            返回2;
        } 别的 {
            返回 火车 (n - 1) + 火车 (n - 2);
        }
    }
    
  5. 这可行,但我们可以简化基本情况:

    int 火车 (int n) {
        如果(n<3){
            返回n;
        } 别的 {
            返回 火车 (n - 1) + 火车 (n - 2);
        }
    }
    
  6. 如果您现在只是将最后一个代码片段粘贴到作业中,而没有完成“太长,没有阅读”预备知识,我已经成功地破坏了你的教育,你永远也学不会编程。不客气。

  7. 这不是计算斐波那契数的最佳方法。为什么?您应该如何修改代码以避免重复工作?是否有可以想象的不同方法?哪些?

Start with the simplest cases and look for regularities.

  1. Train (1) is obviously 1: (1).
  2. Train (2) is obviously 2: (1 1) and (2).
  3. Train (3) is 3: (1 1 1), (1 2), and (2 1). The first two can be combined into conjoining (1) with (1 1) resp (2). The latter are exactly the combinations of Train (2). So, Train (3) is Train (2) + 1.
  4. Train (4) is 5: (1 1 1 1) (1 1 2) (1 2 1) (2 1 1) (2 2). Again, we can combine the first as (1) with (1 1 1), (1 2), and (2 1), which are the combinations of Train (3). The last are (2) conjoined with (1 1) and (2), which are the combinations of Train (2). So, Train (4) is Train (3) + Train (2). Now, looking back at Train (3), we see that the + 1 is Train (1).

Now it seems clear that Train (n) is always Train (n - 1) + Train (n - 2). That is exactly the definition of the Fibonacci sequence.

Now, let us see how that is translated to C.

  1. The function skeleton: Train takes one integer argument and returns an integer:

    int Train (int n) {
    }
    
  2. The definition we worked out:

    int Train (int n) {
        return Train (n - 1) + Train (n - 2);
    }
    
  3. This will recurse infinitely, so we need to stop it at the base case. One base case is clear: Train (1) is 1:

    int Train (int n) {
        if (n == 1) {
            return 1;
        } else {
            return Train (n - 1) + Train (n - 2);
        }
    }
    
  4. This is still not enough. Imagine what Train (2) does: it will calculate Train (1) + Train (0). Train (1) is no problem, but Train (0) will calculate Train (-1) + Train (-2), which again recurses infinitely. So, we need another base case: Train (2) is 2.

    int Train (int n) {
        if (n == 1) {
            return 1;
        } else if (n == 2) {
            return 2;
        } else {
            return Train (n - 1) + Train (n - 2);
        }
    }
    
  5. This works, but we can simplify the base cases:

    int Train (int n) {
        if (n < 3) {
            return n;
        } else {
            return Train (n - 1) + Train (n - 2);
        }
    }
    
  6. If you now just paste that last code snippet into your homework without working through the "too long, didn't read" preliminaries, I have successfully undermined your education, and you will never learn to program. You are welcome.

  7. This is not the best way to calculate Fibonacci numbers. Why? How should you modify the code to avoid duplication of effort? Are there different approaches imaginable? Which ones?

记忆里有你的影子 2024-10-19 02:56:34

这是一个简单的斐波那契数列
对于任何长度为 n 的火车,第一节车的长度可以为 1 或 2。这使我们得到 f(n) = f(n - 1) + f(n - 2)代码>公式。

我可能不需要告诉你如何计算斐波那契数。

That's a simple fibonacci sequence.
For any train of length n first cart can be of length 1 or 2. That brings us to f(n) = f(n - 1) + f(n - 2) formula.

I probably don't have to tell you how to calculate fibonacci numbers.

逆夏时光 2024-10-19 02:56:34

我想这个递归公式可以回答这个问题

if (n <= 2) 返回 n
火车(n)=火车(n-1)+火车(n-2)

I guess this recursive formula answers the question

if (n <= 2) return n
Train(n) = Train(n-1) + Train(n-2)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文