01串

一个数字 ,刚开始是1, 后面每一步进行如下操作:

1替换成0

0替换成01

会得到如下串:

第一次得到 1

第二次得到 01

第三次得到 010

第四次得到 01001

求第n次得到的数字中从左到右第p位数字是几?

n<100

思路一:

直接拼接:

https://www.geeksforgeeks.org/fibonacci-word/

// program for nth Fibonacci word

#include<bits/stdc++.h>

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;

发现大了根本不行,搜索了一下 Fibonacci Word,有直接生成的方法

访问不了wiki,用下面的连接

https://encyclopedia.thefreedictionary.com/Fibonacci+word

#include<bits/stdc++.h>
using namespace std;

//https://www.geeksforgeeks.org/fibonacci-word/

// 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;
}

// https://encyclopedia.thefreedictionary.com/Fibonacci+word
const double golden_ratio = 1.618;
int calc_nth_fibWord(int n)
{
    double x = 2 + floor(n*golden_ratio)- floor((n + 1)*golden_ratio);
    cout<<x;
    return x;
}
// driver program
int main()
{
    int n = 6;
    cout << fibWord(n) <<endl;
    for(int i=1;i<=21;i++)
    {
        calc_nth_fibWord(i);
    }
    cout<<endl;

    return 0;
}

计算出来和原来的对比,是一样的

posted on 2020-05-25 16:55  katago  阅读(643)  评论(0编辑  收藏  举报