C语言分型算法,求助“分形”递归算法???高手请进!!!

using System;

using System.Collections.Generic;

using System.Text;

namespace example1

{

class Program

{   //n为最大值7时,分形的大小是3的6次方即729

static int maxscale = 20000;

static void Main(string[] args)

{

int n;//分形的大小

int i, j; //循环变量

char[,] Fractal = new char[maxscale, maxscale];

while (true)

{

n = Convert.ToInt32(Console.ReadLine());

if (n == -1)

break;

int measure = (int)pow(3, n - 1);

setFractl(Fractal,0,0,n);

for (i = 0; i < measure; i++)

{

int max = 0;

for (j = 0; j < measure; j++) //找到每行最后一个X

{

if (Fractal[i, j] == 'X')

max = j;

}

for (j = 0; j < max; j++)  //非X位置为空格

{

if (Fractal[i, j] != 'X')

{

Fractal[i, j] = ' ';

}

}

Fractal[i, max+1] = 'o';

}

//输出图形

for (i = 0; i < measure; i++)

{

for (j = 0; j < measure; j++)

{

if (Fractal[i, j] != 'o')

{

Console.Write(Fractal[i, j]);

}

else

{

break;

}

}

Console.WriteLine();

}

Console.WriteLine("------------------------------------------");

}

Console.ReadLine();

}

//函数功能:从(startX,startY)位置开始设置度数为n的盒形分形

//即对盒型分形中的每个X,在字符数组Frac的相应位置设置字符“X”

public static void setFractl(char[,] Frac, int startX, int startY, int n)

{

if (n == 1)

{

Frac[startX, startY] = 'X';

}

else

{

int L0 = (int)pow(3, n - 2);

setFractl(Frac,startX+0,startY+0,n-1);

setFractl(Frac,startX+2*L0,startY+0,n-1);

setFractl(Frac,startX+L0,startY+L0,n-1);

setFractl(Frac,startX+0,startY+2*L0,n-1);

setFractl(Frac,startX+2*L0,startY+2*L0,n-1);

}

}

public static int pow(int x, int y)

{

int result = 1;

for (int i = 0; i < y; i++)

{

result = result * x;

}

return result;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值