📌  相关文章
📜  将X和Y放置在n个地方以使两个X都不在一起的方式总数

📅  最后修改于: 2021-05-04 22:09:36             🧑  作者: Mango

给定N个位置,任务是计算放置X和Y的方式总数,以使没有两个X在一起。
例子:

Input: 3
Output: 5
XYX, YYX, YXY, XYY and YYY 

Input: 4
Output: 8
XYXY, XYYX, YXYX, YYYX, YYXY, YXYY, XYYY and YYYY

方法:
对于N = 1,X和Y是2种可能的方式。
对于N = 2,XY,YX和YY是3种可能的方式。
对于N = 3,XYX,YYX,YXY,XYY和YYY是5种可能的方式。
对于N = 4,XYXY,XYYX,YXYX,YYYX,YYXY,YXYY,XYYY和YYYY是8种可能的方式。
在求解N值时,观察到斐波那契图案系列。
下面是上述方法的迭代实现:

C++
// C++ program to find the
// number of ways Calculate
// total ways to place 'x'
// and 'y' at n places such
// that no two 'x' are together
#include 
using namespace std;
 
    // Function to return
    // number of ways
    int ways(int n)
    {
        // for n=1
        int first = 2;
 
        // for n=2
        int second = 3;
        int res = 0;
 
        // iterate to find
        // Fibonacci term
        for (int i = 3; i <= n; i++)
        {
            res = first + second;
            first = second;
            second = res;
        }
 
        return res;
    }
 
// Driver Code
int main()
{
    // total number of places
    int n = 7;
    cout << "Total ways are : ";
    cout << ways(n);
 
    return 0;
}
 
// This code is contributed
// by jit_t


Java
// Java program to find the number of ways
// Calculate total ways to place 'x' and 'y'
// at n places such that no two 'x' are together
public class GFG {
     
    // Function to return number of ways
    static int ways(int n)
    {
        // for n=1
        int first = 2;
 
        // for n=2
        int second = 3;
        int res = 0;
 
        // iterate to find Fibonacci term
        for (int i = 3; i <= n; i++) {
            res = first + second;
            first = second;
            second = res;
        }
 
        return res;
    }
    public static void main(String[] args)
    {
 
        // total number of places
        int n = 7;
 
        System.out.print("Total ways are: " + ways(n));
    }
}


Python3
# Python3 program to find the
# number of ways Calculate
# total ways to place 'x'
# and 'y' at n places such
# that no two 'x' are together
 
# Function to return
# number of ways
def ways(n):
     
    # for n=1
    first = 2;
 
    # for n=2
    second = 3;
    res = 0;
 
    # iterate to find
    # Fibonacci term
    for i in range(3, n + 1):
        res = first + second;
        first = second;
        second = res;
 
    return res;
     
# Driver Code
 
# total number of places
n = 7;
print("Total ways are: " ,
                 ways(n));
 
# This code is contributed by mits


C#
// C# program to find the
// number of ways. Calculate
// total ways to place 'x'
// and 'y' at n places such
// that no two 'x' are together
using System;
 
class GFG
{
     
    // Function to return
    // number of ways
    static int ways(int n)
    {
        // for n=1
        int first = 2;
 
        // for n=2
        int second = 3;
        int res = 0;
 
        // iterate to find
        // Fibonacci term
        for (int i = 3; i <= n; i++)
        {
            res = first + second;
            first = second;
            second = res;
        }
 
        return res;
    }
     
    // Driver Code
    static public void Main ()
    {
         
        // total number
        // of places
        int n = 7;
 
        Console.WriteLine("Total ways are: " +
                                     ways(n));
    }
}
 
//This code is contributed by ajit


PHP


Javascript


输出:
Total ways are: 34

时间复杂度: O(N)