描述
Consider a regular triangular area, divide it into four equal triangles of half height and remove the one in the middle. Apply the same operation recursively to each of the three remaining triangles. If we repeated this procedure infinite times, we'd obtain something with an area of zero. The fractal that evolves this way is called the Sierpinski Triangle. Although its topological dimension is 2, its Hausdorff-Besicovitch dimension is log(3)/log(2)~1.58, a fractional value (that's why it is called a fractal). By the way, the Hausdorff-Besicovitch dimension of the Norwegian coast is approximately 1.52, its topological dimension being 1.
For this problem, you are to outline the Sierpinski Triangle up to a certain recursion depth, using just ASCII characters. Since the drawing resolution is thus fixed, you'll need to grow the picture appropriately. Draw the smallest triangle (that is not divided any further) with two slashes, to backslashes and two underscores like this:
/\
/__\
To see how to draw larger triangles, take a look at the sample output.
输入The input contains several testcases. Each is specified by an integer n. Input is terminated by n=0. Otherwise 1<=n<=10 indicates the recursion depth.输出For each test case draw an outline of the Sierpinski Triangle with a side's total length of 2ncharacters. Align your output to the left, that is, print the bottom leftmost slash into the first column. The output must not contain any trailing blanks. Print an empty line after each test case.样例输入
3 2 1 0
样例输出
/\ /__\ /\ /\ /__\/__\ /\ /\ /__\ /__\ /\ /\ /\ /\ /__\/__\/__\/__\ /\ /__\ /\ /\ /__\/__\ /\ /__\
提示
![](https://i-blog.csdnimg.cn/blog_migrate/5ca3f5270fbb076af52cc0666280716b.jpeg)
The Sierpinski-Triangle up to recursion depth 7
来源Ulm Local 2002
这道题的思路是
reduction(x, y, n)表示在(x,y)点为左小角画一个 深度为n 分形
则易得递归式
PE的原因是因为没有读题,在尾部多输出了许多training blanks.
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream> #include <cstring> using namespace std; char tmp[6000][3000]; void draw(int n){ for (int i = 0; i < (2 << (n - 1)); i++){ int end = 0; for ( int k = ( 2 << n )- 1; ; k --){ if (tmp[i][k] == '\\') { end = k; break; } } for ( int j = 0; j <= end; j++){ cout << tmp[i][j]; } cout << endl; } cout << endl; return; } void reduction( int x, int y, int n ){ if ( n == 1){ tmp[x][y] = tmp[x - 1][y + 1] = '/'; tmp[x][y + 3] = tmp[x - 1][y + 2] = '\\'; tmp[x][y + 1] = tmp[x][y + 2] = '_'; return; } int size = 1 << ( n - 1); reduction( x - size , y + size , n - 1); reduction( x , y , n - 1); reduction( x , y + size + size, n - 1); } int main(){ int n = 0; while ( cin >> n && n != 0 ){ memset ( tmp, ' ' ,sizeof(tmp)); reduction( (1 << n) - 1 , 0, n ); draw ( n ); } return 0; }