Dado un tablero de “2 x n” y fichas de tamaño “2 x 1”, cuente el número de formas de colocar mosaicos en el tablero dado usando las fichas de 2 x 1. Una loseta puede colocarse horizontalmente, es decir, como una loseta de 1 x 2, o verticalmente, es decir, como una loseta de 2 x 1.
Ejemplos:
C++
// C++ program to count the // no. of ways to place 2*1 size // tiles in 2*n size board. #include <iostream> using namespace std; int getNoOfWays(int n) { // Base case if (n <= 2) return n; return getNoOfWays(n - 1) + getNoOfWays(n - 2); } // Driver Function int main() { cout << getNoOfWays(4) << endl; cout << getNoOfWays(3); return 0; }
Java
/* Java program to count the no of ways to place 2*1 size tiles in 2*n size board. */ import java.io.*; class GFG { static int getNoOfWays(int n) { // Base case if (n <= 2) { return n; } return getNoOfWays(n - 1) + getNoOfWays(n - 2); } // Driver Function public static void main(String[] args) { System.out.println(getNoOfWays(4)); System.out.println(getNoOfWays(3)); } } // This code is contributed by ashwinaditya21.
Python3
# Python3 program to count the # no. of ways to place 2*1 size # tiles in 2*n size board. def getNoOfWays(n): # Base case if n <= 2: return n return getNoOfWays(n - 1) + getNoOfWays(n - 2) # Driver Code print(getNoOfWays(4)) print(getNoOfWays(3)) # This code is contributed by Kevin Joshi
C#
// C# program to implement // the above approach using System; class GFG { static int getNoOfWays(int n) { // Base case if (n <= 2) { return n; } return getNoOfWays(n - 1) + getNoOfWays(n - 2); } // Driver Code public static void Main() { Console.WriteLine(getNoOfWays(4)); Console.WriteLine(getNoOfWays(3)); } } // This code is contributed by code_hunt.
Javascript
<script> // JavaScript program to count the // no. of ways to place 2*1 size // tiles in 2*n size board. function getNoOfWays(n) { // Base case if (n <= 2) return n; return getNoOfWays(n - 1) + getNoOfWays(n - 2); } // Driver Function document.write(getNoOfWays(4)); document.write(getNoOfWays(3)); // This code is contributed by shinjanpatra </script>
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA