# The Sierpinski Fractal — 递归

## C:The Sierpinski Fractal

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 2n characters. 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
```

```       /\
/__\
/\  /\
/__\/__\
/\      /\
/__\    /__\
/\  /\  /\  /\
/__\/__\/__\/__\

/\
/__\
/\  /\
/__\/__\

/\
/__\
```

``````#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define inf 1000000007
#define ll long long
#define N 1040
inline int rd()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
char s[N][N<<1],a={{' ','/','\\','\0'},{'/','_','_','\\'}};
int n;
void sol(int p,int x,int y){
if(p==0){
for(int i=0;i<4;++i){
s[x][y+i]=a[i];
s[x+1][y+i]=a[i];
}
return;
}
int len=1<<p;
sol(p-1,x,y+len);sol(p-1,x+len,y);sol(p-1,x+len,y+(len<<1));
}
int main()
{
while(true){
n=rd()-1;
if(n==-1) return 0;
memset(s,0,sizeof(s));
sol(n,0,0);
for(int i=0;i<(2<<n);++i){
for(int j=0;j<(2<<n)+i;++j) if(!s[i][j]) s[i][j]=' ';
printf("%s\n",s[i]);
}
puts("");
}
return 0;
}
``````