Sweety

Practice makes perfect

导航

C - Fractal(3.4.1)

Posted on 2014-07-17 17:34  蓝空  阅读(182)  评论(0编辑  收藏  举报

Description

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.

Input

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.

Output

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.

Sample Input

1
2
3
4
-1

Sample Output

X
-
X X
 X
X X
-
X X   X X
 X     X
X X   X X
   X X
    X
   X X
X X   X X
 X     X
X X   X X
-
X X   X X         X X   X X
 X     X           X     X
X X   X X         X X   X X
   X X               X X
    X                 X
   X X               X X
X X   X X         X X   X X
 X     X           X     X
X X   X X         X X   X X
         X X   X X
          X     X
         X X   X X
            X X
             X
            X X
         X X   X X
          X     X
         X X   X X
X X   X X         X X   X X
 X     X           X     X
X X   X X         X X   X X
   X X               X X
    X                 X
   X X               X X
X X   X X         X X   X X
 X     X           X     X
X X   X X         X X   X X
-

 

对这样的递归图形问题,只是简单的将其写出函数即找出规律,然后找出边界,然后写成递归就行了

有点逗比的是将题目中的-写成了_,wr了好多次!!!!    

 

本题使用函数pow(a,b)即a的b次方

#include <iostream>
#include <cstdio>
#include <memory.h>
#include <cmath>
using namespace std;
char map[1000][1000];
void paint(int n,int x,int y){
 if(n==1){
  map[x][y]='X';
  return;
 }
 int size = (int)pow(3.0,n-2);    
 paint(n-1, x, y);      //左上角
 paint(n-1, x, y+size*2);   //右上角
 paint(n-1, x+size, y+size);         //中間
 paint(n-1, x+size*2, y);            //左下角
 paint(n-1, x+size*2, y+size*2); //右下角
}
int main(){
	int n;
	while (~scanf("%d",&n)&&n!=-1){
	  int size=(int)pow(3.0, n-1);       //度为n的分形图的规模是3^(n-1)
	  memset(map,' ',sizeof(map));
	  paint(n, 1, 1);
	  for (int i=1;i<=size;i++ )   {   //列印         
	    for(int j=1;j<=size;j++)
	     cout<<map[i][j];
	    cout<<endl;         
	  }
	   cout<<'-'<<endl;
	}
 return 0;
}