📜  斐波那契字

📅  最后修改于: 2022-05-13 01:57:08.924000             🧑  作者: Mango

斐波那契字

就像斐波那契数,一个斐波那契字。是二进制数字的特定序列(或任何两个字母的符号)。斐波那契字是通过重复连接形成的,就像斐波那契数是通过重复加法形成的一样。但与斐波那契数不同的是,斐波那契词的前两个术语彼此不同。

In Fibonacci word,
  S(0) = 0, 
  S(1) = 01, 
  S(2) = 010,
  S(3) = 01001
   ..... 
where S(n) = S(n-1) + S(n-2) and + 
represents the concatenation of 
strings. 

任务是找到给定数字 n 的第 n 个斐波那契字。
例子:

Input : n = 4
Output : S(4) = 01001010

Input : n = 2
Output : S(2) = 010

就像在斐波那契数的程序中一样,我们在这里使用寻找第 n 个斐波那契数的迭代概念来寻找第 n 个斐波那契词,我们可以使用迭代概念。因此,为了找到第 n 个斐波那契字,我们将采用两个字符串Sn 和 Sn_1,它们分别代表 S(n) 和 S(n-1),并且在每次迭代中,我们将更新 tmp = Sn、Sn = Sn + Sn_1 和 Sn_1 = tmp这样我们就可以找到第n个斐波那契字了。

C++
// program for nth Fibonacci word
#include
using namespace std;
 
// Returns n-th Fibonacci word
string fibWord(int n)
{
    string Sn_1 = "0";
    string Sn = "01";
    string tmp;
    for (int i=2; i<=n; i++)
    {
        tmp = Sn;
        Sn += Sn_1;
        Sn_1 = tmp;
    }
 
    return Sn;
}
 
// driver program
int main()
{
    int n = 6;
    cout << fibWord(n);
    return 0;
}


Java
// Java program for nth Fibonacci word
import java.util.*;
 
class Eulerian
{
    // Returns n-th Fibonacci word
    public static String fibWord(int n)
    {
        String Sn_1 = "0";
        String Sn = "01";
        String tmp;
        for (int i=2; i<=n; i++)
        {
            tmp = Sn;
            Sn += Sn_1;
            Sn_1 = tmp;
        }
 
        return Sn;
    }
     
    // driver code
    public static void main(String[] args)
    {
        int n = 6;
        System.out.print(fibWord(n));
    }
}
 
// This code is contributed by rishabh_jain


Python3
# Python3 program for nth Fibonacci word
 
# Returns n-th Fibonacci word
def fibWord(n):
    Sn_1 = "0"
    Sn = "01"
    tmp = ""
    for i in range(2, n + 1):
        tmp = Sn
        Sn += Sn_1
        Sn_1 = tmp
    return Sn
 
# driver program
n = 6
print (fibWord(n))
 
# This code is contributed by Sachin Bisht


C#
// C# program for nth Fibonacci word
using System;
 
class GFG
{
    // Returns n-th Fibonacci word
    public static String fibWord(int n)
    {
        String Sn_1 = "0";
        String Sn = "01";
        String tmp;
        for (int i = 2; i <= n; i++)
        {
            tmp = Sn;
            Sn += Sn_1;
            Sn_1 = tmp;
        }
 
        return Sn;
    }
     
    // Driver code
    public static void Main()
    {
        int n = 6;
        Console.WriteLine(fibWord(n));
    }
}
 
// This code is contributed by vt_m


Javascript


输出:

010010100100101001010