1085:Fractal
-
时间限制:
- 1000ms
- 65536kB
-
描述
-
A fractal is an object or quantity that displays self-similarity, in a somewhat technical sense, on all scales. The object need not exhibit exactly the same structure at all scales, but the same "type" of structures must appear on all scales.
A box fractal is defined as below :
- A box fractal of degree 1 is simply
X - A box fractal of degree 2 is
XX
X
XX - If using B(n - 1) to represent the box fractal of degree n - 1, then a box fractal of degree n is defined recursively as following
B(n - 1) B(n - 1)
B(n - 1)
B(n - 1) B(n - 1)
Your task is to draw a box fractal of degree n.
输入
- A box fractal of degree 1 is simply
- The input consists of several test cases. Each line of the input contains a positive integer n which is no greater than 7. The last line of input is a negative integer −1 indicating the end of input. 输出
- For each test case, output the box fractal using the 'X' notation. Please notice that 'X' is an uppercase letter. Print a line with only a single dash after each test case.
实验源码(OpenJudge提交通过)
#include <stdio.h>
char box[730][730]={"",""};
void drawFractalBox(int n,int x,int y)//x为行,y为列
{
int i=n;
int c=1;
while(--i)
c=c*3;
//首先计算打印的行数和列数 C
if(c==1)
{
box[x][y]='X';
return;
}
else
{
drawFractalBox(n-1,x,y); //左上
drawFractalBox(n-1,x,(c*2)/3+y); //右上
drawFractalBox(n-1,c/3+x,c/3+y); //中间
drawFractalBox(n-1,(c*2)/3+x,y); //左下
drawFractalBox(n-1,(2*c)/3+x,y+(2*c)/3); //右下
}
}
int main()
{
int i,j,n;
scanf("%d",&n);
while(n!=-1)
{
int num=1;
int t=n;
while(--t)
num=num*3; //得到行列数
for(i=1;i<=num;i++)
for(j=1;j<=num;j++)
box[i][j]=' '; //初始化
drawFractalBox(n,1,1); //填充
for(i=1;i<=num;i++)
{
for(j=1;j<=num;j++)
printf("%c",box[i][j]);
printf("\n");
}//输出
printf("-\n");
scanf("%d",&n);//输入下一个
}
return 0;
}
运行结果截图: