寻找具有常数和的 1 和 2 的可能组合
这是用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从最简单的案例开始,寻找规律。
Train (1)
显然是 1: (1)。Train (2)
显然是 2:(1 1) 和 (2)。训练 (3)
是 3:(1 1 1)、(1 2) 和 (2 1)。前两者可以组合成连接 (1) 和 (1 1) 或 (2)。后者正是Train (2)
的组合。因此,Train (3)
是Train (2) + 1
。火车 (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)
,我们看到+ 1
是Train (1)
。现在看来很明显,
Train (n)
始终是Train (n - 1) + Train (n - 2)
。这正是斐波那契数列的定义。现在,让我们看看如何将其转换为 C。
函数骨架:
Train
接受一个整数参数并返回一个整数:我们得出的定义:
这将无限递归,因此我们需要在基本情况下停止它。一个基本情况是明确的:
Train (1)
是 1:这仍然不够。想象一下
Train (2)
的作用:它将计算Train (1) + Train (0)
。Train (1)
没问题,但是Train (0)
会计算Train (-1) + Train (-2)
,这又是无限递归。因此,我们需要另一个基本情况:Train (2)
是 2。这可行,但我们可以简化基本情况:
如果您现在只是将最后一个代码片段粘贴到作业中,而没有完成“太长,没有阅读”预备知识,我已经成功地破坏了你的教育,你永远也学不会编程。不客气。
这不是计算斐波那契数的最佳方法。为什么?您应该如何修改代码以避免重复工作?是否有可以想象的不同方法?哪些?
Start with the simplest cases and look for regularities.
Train (1)
is obviously 1: (1).Train (2)
is obviously 2: (1 1) and (2).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 ofTrain (2)
. So,Train (3)
isTrain (2) + 1
.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 ofTrain (3)
. The last are (2) conjoined with (1 1) and (2), which are the combinations ofTrain (2)
. So,Train (4)
isTrain (3) + Train (2)
. Now, looking back atTrain (3)
, we see that the+ 1
isTrain (1)
.Now it seems clear that
Train (n)
is alwaysTrain (n - 1) + Train (n - 2)
. That is exactly the definition of the Fibonacci sequence.Now, let us see how that is translated to C.
The function skeleton:
Train
takes one integer argument and returns an integer:The definition we worked out:
This will recurse infinitely, so we need to stop it at the base case. One base case is clear:
Train (1)
is 1:This is still not enough. Imagine what
Train (2)
does: it will calculateTrain (1) + Train (0)
.Train (1)
is no problem, butTrain (0)
will calculateTrain (-1) + Train (-2)
, which again recurses infinitely. So, we need another base case:Train (2)
is 2.This works, but we can simplify the base cases:
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.
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?
这是一个简单的斐波那契数列。
对于任何长度为
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 tof(n) = f(n - 1) + f(n - 2)
formula.I probably don't have to tell you how to calculate fibonacci numbers.
我想这个递归公式可以回答这个问题
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)