Sierpinski三角形是一个分形且有吸引力的固定集,整体形状为三角形 等边三角形 .它递归地细分成更小的三角形。
null
例如:
Input : n = 4Output : * * * * * * * * * Input : n = 8Output : * * * * * * * * * * * * * * * * * * * * * * * * * * *
方法:
Sierpinski三角形将通过重复删除三角形子集从等边三角形构造而成。 施工步骤: 1.取任意等边三角形。 2.将其分成4个较小的全等三角形,并移除中心三角形。 3.对剩余的每个小三角形重复步骤2。
下面是实现sierpinski三角形的程序
C++
// C++ program to print sierpinski triangle. #include <bits/stdc++.h> using namespace std; void printSierpinski( int n) { for ( int y = n - 1; y >= 0; y--) { // printing space till // the value of y for ( int i = 0; i < y; i++) { cout<< " " ; } // printing '*' for ( int x = 0; x + y < n; x++) { // printing '*' at the appropriate position // is done by the and value of x and y // wherever value is 0 we have printed '*' if (x & y) cout<< " " << " " ; else cout<< "* " ; } cout<<endl; } } // Driver code int main() { int n = 16; // Function calling printSierpinski(n); return 0; } |
JAVA
// Java program to print // sierpinski triangle. import java.util.*; import java.io.*; class GFG { static void printSierpinski( int n) { for ( int y = n - 1 ; y >= 0 ; y--) { // printing space till // the value of y for ( int i = 0 ; i < y; i++) { System.out.print( " " ); } // printing '*' for ( int x = 0 ; x + y < n; x++) { // printing '*' at the appropriate // position is done by the and // value of x and y wherever value // is 0 we have printed '*' if ((x & y) != 0 ) System.out.print( " " + " " ); else System.out.print( "* " ); } System.out.print( "" ); } } // Driver code public static void main(String args[]) { int n = 16 ; // Function calling printSierpinski(n); } } // This code is contributed by Sahil_Bansall |
Python3
# Python 3 program to print # sierpinski triangle. def printSierpinski( n) : y = n - 1 while (y > = 0 ) : # printing space till # the value of y i = 0 while (i < y ): print ( " " ,end = "") i = i + 1 # printing '*' x = 0 while (x + y < n ): # printing '*' at the appropriate # position is done by the and # value of x and y wherever value # is 0 we have printed '*' if ((x & y) ! = 0 ) : print ( " " , end = " " ) else : print ( "* " , end = "") x = x + 1 print () y = y - 1 # Driver code n = 16 # Function calling printSierpinski(n) # This code is contributed by Nikita Tiwari. |
C#
// C# program to print // sierpinski triangle. using System; class GFG { static void printSierpinski( int n) { for ( int y = n - 1; y >= 0; y--) { // printing space till // the value of y for ( int i = 0; i < y; i++) { Console.Write( " " ); } // printing '*' for ( int x = 0; x + y < n; x++) { // printing '*' at the appropriate // position is done by the and // value of x and y wherever value // is 0 we have printed '*' if ((x & y) != 0) Console.Write( " " + " " ); else Console.Write( "* " ); } Console.WriteLine(); } } // Driver code public static void Main() { int n = 16; // Function calling printSierpinski(n); } } // This code is contributed by vt_m |
PHP
<?php // PHP implementation to // print sierpinski triangle. function printSierpinski( $n ) { for ( $y = $n - 1; $y >= 0; $y --) { // printing space till // the value of y for ( $i = 0; $i < $y ; $i ++) { echo " " ; } // printing '*' for ( $x = 0; $x + $y < $n ; $x ++) { // printing '*' at the appropriate // position is done by the and value // of x and y wherever value is 0 we // have printed '*' if ( $x & $y ) echo " " ; else echo "* " ; } echo "" ; } } // Driver code $n = 16; printSierpinski( $n ); // This code is contributed by Mithun Kumar ?> |
Javascript
<script> // javascript program to print // sierpinski triangle. function printSierpinski(n) { for ( var y = n - 1; y >= 0; y--) { // printing space till // the value of y for ( var i = 0; i < y; i++) { document.write( " " ); } // printing '*' for ( var x = 0; x + y < n; x++) { // printing '*' at the appropriate // position is done by the and // value of x and y wherever value // is 0 we have printed '*' if ((x & y) != 0) document.write( " " ); else document.write( "* " ); } document.write( "<br>" ); } } // Driver code var n = 16; // Function calling printSierpinski(n); // This code contributed by Princi Singh </script> |
输出:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
参考资料: 维基
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END