原文:

是生成以下整数序列的那个。 1 1 2 3 4 4 5 6 7…..并遵循以下递归公式。

p(n) = p(p(n - 1))   p(n - p(n - 1))

给定一个数字 n,然后打印 n 个纽曼-康威序列项 示例:

input : 13
output : 1 1 2 2 3 4 4 4 5 6 7 7 8
input : 20
output : 1 1 2 2 3 4 4 4 5 6 7 7 8 8 8 8 9 10 11 12 

c

// c   program to print n terms
// of newman-conway sequence
#include 
using namespace std;
// function to find
// the n-th element
void sequence(int n)
{
    // declare array to store sequence
    int f[n   1];
    f[0] = 0;
    f[1] = 1;
    f[2] = 1;
    cout << f[1] << " " << f[2] << " ";
    for (int i = 3; i <= n; i  ) {
        f[i] = f[f[i - 1]]   f[i - f[i - 1]];       
        cout << f[i] << " ";
    }
}
// driver program
int main()
{   
    int n = 13;   
    sequence(n);   
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to print n terms
// of newman-conway sequence
class gfg
{
    // function to find
    // the n-th element
    public static void sequence(int n)
    {
        // declare array to store sequence
        int f[] = new int[n   1];
        f[0] = 0;
        f[1] = 1;
        f[2] = 1;
        system.out.print( f[1]   " "   f[2]   " ");
        for (int i = 3; i <= n; i  )
        {
            f[i] = f[f[i - 1]]   f[i - f[i - 1]];    
            system.out.print(f[i]   " ");
        }
    }
    //driver code
    public static void main(string []args)
    {
        int n = 13 ;
        sequence(n);
    }
}
// this program is contributed
// by upendra singh bartwal

python 3

# python program to print n terms
# of newman-conway sequence
def sequence(n):
    # function to find
    # the n-th element
    # declare array to store sequence
    f = [0, 1, 1]
    print(f[1], end=" "),
    print(f[2], end=" "),
    for i in range(3,n 1):
        f.append( f[f[i - 1]]   f[i - f[i - 1]])
        print(f[i], end=" "),
# driver code
n = 13
sequence(n)
# this code is contributed
# by upendra singh bartwal

c

// c# program to print n terms
// of newman-conway sequence
using system;
class gfg
{
    // function to find
    // the n-th element
    public static void sequence(int n)
    {
        // declare array to store sequence
        int []f = new int[n   1];
        f[0] = 0;
        f[1] = 1;
        f[2] = 1;
        console.write( f[1]   " "   f[2]   " ");
        for (int i = 3; i <= n; i  )
        {
            f[i] = f[f[i - 1]]   f[i - f[i - 1]];
            console.write(f[i]   " ");
        }
    }
    // driver code
    public static void main()
    {
        int n = 13 ;
        sequence(n);
    }
}
// this program is contributed
// by vt_m.

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


输出:

1 1 2 2 3 4 4 4 5 6 7 7 8