Please read the new rule regarding the restriction on the use of AI tools. ×

black.af's blog

By black.af, history, 4 years ago, In English

[Your text to link here...]Hi everyone! I am a beginner in programming and I would like to have your opinion on this problem. In my opinion the program should return the number of partition of the number of bricks n (those containing only 1 and 2). On the other hand it seems that the sequence of bricks looks like a Fibonacci sequence. I am a bit confused. Please help me!

We build a wall by the brick, the height of the brick is twice as long as the width of the brick, and the height of the wall is equal to the height of the brick. Observe the figure below , 2 bricks has two possible building methods, 3 bricks has three possible building methods. Please design a program, input an non-negative integer less than 40, output the number of possible building methods. When entering 0, ends the loop. Input Your program receives a sequence of positive integers, one per line, each representing the length of a wall. The maximum value for the wall is length 40. The input terminates with a 0 Output For each wall length given in the input, your program must output the corresponding number of different patterns for such a wall in a separate line. See picture below !

  • Vote: I like it
  • -23
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
Rev. 2   Vote: I like it +3 Vote: I do not like it

Let's call $$$F(n)=$$$ the number of way to build a way with length n. As you already draw, we have $$$F(1) = 1, F(2) = 2$$$, which is the first 2 Fibonacci numbers. Let's see how can we calculate $$$F(n)$$$ using $$$F(n - 1)$$$, $$$F(n - 2)$$$, since you already realized this is the Fibonacci sequence. Well, if we just place a brick vertically at the end, then we need to build the rest of the wall, which is the same as building a wall of length $$$n - 1$$$, so in this case, it is $$$F(n - 1)$$$. The other case is, you put 2 bricks horizontally on top of each other, so the rest is a wall with length $$$n - 2$$$, hence we have $$$F(n - 2)$$$ way to build in this case. So total cases for $$$F(n)$$$ are $$$F(n - 1) + F(n - 2)$$$.

Also next time please format your text carefully instead of just copying and pasting. I can see you put the input and output format in the same paragraph as the statement with no punctuation.