01941: The Sierpinski Fractal
http://cs101.openjudge.cn/practice/01941/
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样例输出
/\
/__\
/\ /\
/__\/__\
/\ /\
/__\ /__\
/\ /\ /\ /\
/__\/__\/__\/__\
/\
/__\
/\ /\
/__\/__\
/\
/__\来源
Ulm Local 2002
Sierpinski三角形是一种著名的分形图案,它是通过不断地在等边三角形中剔除中心的小三角形而形成的。
函数f(n)是一个递归函数,用于生成深度为n的Sierpinski三角形。当n为1时,它返回一个包含两个字符串的列表,这两个字符串组成了一个最小的Sierpinski三角形。当n大于1时,它首先调用自身生成深度为n-1的Sierpinski三角形,然后在这个基础上构造深度为n的Sierpinski三角形。
构造过程分为两步:首先,对于深度为n-1的Sierpinski三角形的每一行,它在左右两侧添加了x个空格,其中x等于2的n-1次方;然后,它将深度为n-1的Sierpinski三角形复制一份,并将两份拼接在一起,形成深度为n的Sierpinski三角形的下半部分。这两步操作完成了深度为n的Sierpinski三角形的构造。
def f(n):
if n == 1:
return [' /\\ ', '/__\\']
t = f(n - 1)
x = 2 ** (n - 1)
res = [' ' * x + u + ' ' * x for u in t]
res.extend([u + u for u in t])
return res
al = [f(i) for i in range(1, 11)]
while True:
n = int(input())
if n == 0:
break
for u in al[n - 1]:
print(u)
print()