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;
}
}
}