1085:Fractal(分治法)

本文介绍了一个使用递归方法绘制分形盒子的C语言程序。该程序能够根据输入的度数生成不同复杂度的分形图案,并使用X字符来表示每个元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
    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 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.
输入
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;
}
运行结果截图:
1085:Fractal(分治法)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值