龙曲线序列是一个0和1的无限二进制序列。序列的第一项是1。
从下一项开始,我们在上一项的每个元素之间交替插入1和0。
为了更好地理解,请参考以下说明:
- 1 (starts with 1)
- “1” 1 “0”
1 and 0 are inserted alternately to the left and right of the previous term. Here the number in the double quotes represents the newly added elements.So the second term becomes
1 1 0 - “1” 1 “0” 1 “1” 0 “0”
So the third term becomes
1 1 0 1 1 0 0 - “1” 1 “0” 1 “1” 0 “0” 1 “1” 1 “0” 0 “1” 0 “0”
The fourth term becomes
1 1 0 1 1 0 0 1 1 1 0 0 1 0 0
The first few terms are:
这也被称为常规折纸顺序。给定自然数n 。任务是找到由龙形曲线形成的第n个字符串,其长度为 。
例子:
Input: n = 4
Output: 110110011100100
Explanation:
We get 1 as the first term,
"110" as the second term,
"1101100" as the third term ,
And hence our fourth term will be
"110110011100100"
Input: n = 3
Output: 1101100
方法:从第一个术语1开始。然后在前一个术语的每个元素之后交替添加1和0。获得的新术语将成为当前术语。从1到n的循环中重复此过程,以生成每个项,最后生成第n个项。
下面是上述想法的实现:
C++
// CPP code to find nth term
// of the Dragon Curve Sequence
#include
using namespace std;
// function to generate the nth term
string Dragon_Curve_Sequence(int n)
{
// first term
string s = "1";
// generating each term of the sequence
for (int i = 2; i <= n; i++)
{
string temp = "1";
char prev = '1', zero = '0', one = '1';
// loop to generate the ith term
for (int j = 0; j < s.length(); j++)
{
// add character from the
// original string
temp += s[j];
// add alternate 0 and 1 in between
if (prev == '0')
{
// if previous added term
// was '0' then add '1'
temp += one;
// now current term becomes
// previous term
prev = one;
}
else
{
// if previous added term
// was '1', then add '0'
temp += zero;
// now current term becomes
// previous term
prev = zero;
}
}
// s becomes the ith term of the sequence
s = temp;
}
return s;
}
// Driver program
int main()
{
// Taking inputs
int n = 4;
// generate nth term of dragon curve sequence
string s = Dragon_Curve_Sequence(n);
// Printing output
cout << s << "\n";
}
Java
// Java code to find nth term
// of the Dragon Curve Sequence
import java.util.*;
class solution
{
// function to generate the nth term
static String Dragon_Curve_Sequence(int n)
{
// first term
String s = "1";
// generating each term of the sequence
for (int i = 2; i <= n; i++)
{
String temp = "1";
char prev = '1', zero = '0', one = '1';
// loop to generate the ith term
for (int j = 0; j < s.length(); j++)
{
// add character from the
// original string
temp += s.charAt(j);
// add alternate 0 and 1 in between
if (prev == '0')
{
// if previous added term
// was '0' then add '1'
temp += one;
// now current term becomes
// previous term
prev = one;
}
else
{
// if previous added term
// was '1', then add '0'
temp += zero;
// now current term becomes
// previous term
prev = zero;
}
}
// s becomes the ith term of the sequence
s = temp;
}
return s;
}
// Driver program
public static void main(String args[])
{
// Taking inputs
int n = 4;
// generate nth term of dragon curve sequence
String s = Dragon_Curve_Sequence(n);
// Printing output
System.out.println(s);
}
}
//This code is contributed by
//Surendra_Gangwar
Python
# Python code to find nth term
# of the Dragon Curve Sequence
# function to generate
# the nth term
def Dragon_Curve_Sequence(n):
# first term
s = "1"
# generating each term
# of the sequence
for i in range(2, n + 1):
temp = "1"
prev = '1'
zero = '0'
one = '1'
# loop to generate the ith term
for j in range(len(s)):
# add character from the
# original string
temp += s[j]
# add alternate 0 and
# 1 in between
if (prev == '0'):
# if previous added term
# was '0' then add '1'
temp += one
# now current term becomes
# previous term
prev = one
else:
# if previous added term
# was '1', then add '0'
temp += zero
# now current term becomes
# previous term
prev = zero
# s becomes the ith term
# of the sequence
s = temp
return s
# Driver Code
n = 4
# generate nth term of
# dragon curve sequence
s = Dragon_Curve_Sequence(n)
# Printing output
print(s)
# This code is contributed by
# Sanjit_Prasad
C#
// C# code to find nth term
// of the Dragon Curve Sequence
using System;
class GFG
{
// function to generate the nth term
static String Dragon_Curve_Sequence(int n)
{
// first term
String s = "1";
// generating each term of the sequence
for (int i = 2; i <= n; i++)
{
String temp = "1";
char prev = '1', zero = '0', one = '1';
// loop to generate the ith term
for (int j = 0; j < s.Length; j++)
{
// add character from the
// original string
temp += s[j];
// add alternate 0 and 1 in between
if (prev == '0')
{
// if previous added term
// was '0' then add '1'
temp += one;
// now current term becomes
// previous term
prev = one;
}
else
{
// if previous added term
// was '1', then add '0'
temp += zero;
// now current term becomes
// previous term
prev = zero;
}
}
// s becomes the ith term of the sequence
s = temp;
}
return s;
}
// Driver Code
public static void Main()
{
// Taking inputs
int n = 4;
// generate nth term of dragon
// curve sequence
String s = Dragon_Curve_Sequence(n);
// Printing output
Console.WriteLine(s);
}
}
// This code is contributed by Rajput-Ji
PHP
输出:
110110011100100