斯特恩-布罗科特序列 类似于斐波那契序列,但在生成斐波那契序列的方式上有所不同。 Stern Brocot序列的生成:
null
1.序列的第一和第二个元素是1和1。 2、考虑序列的第二个成员。然后,将所考虑的序列成员和它的先例相加,即(1+1=2)。现在2是我们系列的下一个元素。顺序将是 [ 1, 1, 2 ] 3.在这个元素之后,序列的下一个元素将是我们第二步考虑的元素。现在序列将是 [ 1, 1, 2, 1 ] 4.我们再次执行步骤2,但现在考虑的元素将是2(第三个元素)。所以,序列的下一个数将是所考虑的数和它的先例(2+1=3)之和。现在就要开始了 [ 1, 1, 2, 1, 3 ] 5.与第3步一样,下一个元素将是所考虑的元素,即2。因此,顺序将是 [ 1, 1, 2, 1, 3, 2 ] 6.因此,这个过程将继续,现在下一个考虑的元素将是1(第四个元素)。
下面是打印Stern Brocot序列的简单程序。
C++
// CPP program to print Brocot Sequence #include <bits/stdc++.h> using namespace std; void SternSequenceFunc(vector< int >& BrocotSequence, int n) { // loop to create sequence for ( int i = 1; BrocotSequence.size() < n; i++) { int considered_element = BrocotSequence[i]; int precedent = BrocotSequence[i - 1]; // adding sum of considered element and it's precedent BrocotSequence.push_back(considered_element + precedent); // adding next considered element BrocotSequence.push_back(considered_element); } // printing sequence.. for ( int i = 0; i < 15; ++i) cout << BrocotSequence[i] << " " ; } int main() { int n = 15; vector< int > BrocotSequence; // adding first two element // in the sequence BrocotSequence.push_back(1); BrocotSequence.push_back(1); SternSequenceFunc(BrocotSequence, n); return 0; } |
JAVA
// Java program to print // Brocot Sequence import java.io.*; import java.util.*; class GFG { static void SternSequenceFunc(Vector<Integer> BrocotSequence, int n) { // loop to create sequence for ( int i = 1 ; BrocotSequence.size() < n; i++) { int considered_element = BrocotSequence.get(i); int precedent = BrocotSequence.get(i- 1 ); // adding sum of considered element and it's precedent BrocotSequence.add(considered_element + precedent); // adding next considered element BrocotSequence.add(considered_element); } // printing sequence.. for ( int i = 0 ; i < 15 ; ++i) System.out.print(BrocotSequence.get(i) + " " ); } // Driver code public static void main (String[] args) { int n = 15 ; Vector<Integer> BrocotSequence = new Vector<Integer>(); // adding first two element // in the sequence BrocotSequence.add( 1 ); BrocotSequence.add( 1 ); SternSequenceFunc(BrocotSequence, n); } } // This code is contributed by Gitanjali. |
Python3
# Python program to print # Brocot Sequence import math def SternSequenceFunc(BrocotSequence, n): # loop to create sequence for i in range ( 1 , n): considered_element = BrocotSequence[i] precedent = BrocotSequence[i - 1 ] # adding sum of considered # element and it's precedent BrocotSequence.append(considered_element + precedent) # adding next considered element BrocotSequence.append(considered_element) # printing sequence.. for i in range ( 0 , 15 ): print (BrocotSequence[i] , end = " " ) # Driver code n = 15 BrocotSequence = [] # adding first two element # in the sequence BrocotSequence.append( 1 ) BrocotSequence.append( 1 ) SternSequenceFunc(BrocotSequence, n) # This code is contributed by Gitanjali. |
C#
// C# program to print // Brocot Sequence using System; using System.Collections.Generic; class GFG { static void SternSequenceFunc(List< int > BrocotSequence, int n) { // loop to create sequence for ( int i = 1; BrocotSequence.Count < n; i++) { int considered_element = BrocotSequence[i]; int precedent = BrocotSequence[i - 1]; // adding sum of considered // element and it's precedent BrocotSequence.Add(considered_element + precedent); // adding next // considered element BrocotSequence.Add( considered_element); } // printing sequence.. for ( int i = 0; i < 15; ++i) Console.Write( BrocotSequence[i] + " " ); } // Driver code static void Main () { int n = 15; List< int > BrocotSequence = new List< int >(); // adding first two element // in the sequence BrocotSequence.Add(1); BrocotSequence.Add(1); SternSequenceFunc(BrocotSequence, n); } } // This code is contributed by // Manish Shaw(manishshaw1) |
PHP
<?php // PHP program to // print Brocot Sequence function SternSequenceFunc(& $BrocotSequence , $n ) { // loop to create sequence for ( $i = 1; count ( $BrocotSequence ) < $n ; $i ++) { $considered_element = $BrocotSequence [ $i ]; $precedent = $BrocotSequence [ $i - 1]; // adding sum of considered // element and it's precedent array_push ( $BrocotSequence , $considered_element + $precedent ); // adding next // considered element array_push ( $BrocotSequence , $considered_element ); } // printing sequence.. for ( $i = 0; $i < 15; ++ $i ) echo ( $BrocotSequence [ $i ]. " " ); } // Driver code $n = 15; $BrocotSequence = array (); // adding first two element // in the sequence array_push ( $BrocotSequence , 1); array_push ( $BrocotSequence , 1); SternSequenceFunc( $BrocotSequence , $n ); // This code is contributed by // Manish Shaw(manishshaw1) ?> |
Javascript
<script> // Javascript program to print Brocot Sequence function SternSequenceFunc( BrocotSequence, n) { // loop to create sequence for ( var i = 1; BrocotSequence.length < n; i++) { var considered_element = BrocotSequence[i]; var precedent = BrocotSequence[i - 1]; // adding sum of considered element and it's precedent BrocotSequence.push(considered_element + precedent); // adding next considered element BrocotSequence.push(considered_element); } // printing sequence.. for ( var i = 0; i < 15; ++i) document.write( BrocotSequence[i] + " " ); } var n = 15; var BrocotSequence = []; // adding first two element // in the sequence BrocotSequence.push(1); BrocotSequence.push(1); SternSequenceFunc(BrocotSequence, n); // This code is contributed by rrrtnx. </script> |
输出:
1 1 2 1 3 2 3 1 4 3 5 2 5 3 4
参考资料: github
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END