1085:Fractal(分治法)

来源:互联网 发布:幻想武器知乎 编辑:程序博客网 时间:2024/06/05 04:40

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 notexhibit 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
    X  X
      X
    X  X
  • If using B(n - 1) to represent the box fractal of degree n - 1,then a box fractal of degree n is defined recursively asfollowing
    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.
输入
The input consists of several test cases. Each line of the inputcontains a positive integer n which is no greater than 7. The lastline of input is a negative integer −1 indicating the end ofinput.
输出
For each test case, output the box fractal using the 'X' notation.Please notice that 'X' is an uppercase letter. Print a line withonly 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;
}
运行结果截图:
1085:Fractal(分治法)

原创粉丝点击