当前位置:编程学习 > C/C++ >>

有趣的分形图形-递归和数学方法解决-POJ 2083

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
-1Sample 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


一递归:
先看下较为常规的递归解决:

由于图形是重复的,小的图形只是把大图形的左上角一部分输出,,只计算最大的图形,打表可加快速度。

 

#include <stdio.h>
#include <string.h>
int p[8] = {1,3,9,27,81,243,729};
char map[730][730];
//n当前的图形大小,x,y图形所在的坐标
void print(int n,int x,int y){
	if(n == 0){
		map[x][y] = 'X'; //
		return;
	}
	print(n-1, x, y); //左上
	print(n-1, x+2*p[n-1], y); //右上
	print(n-1, x+p[n-1], y+p[n-1]); //中间
	print(n-1, x, y+2*p[n-1]);
	print(n-1, x+2*p[n-1],  y+2*p[n-1]);
}
int n;
int main(){
	for(int i=0; i<p[6]; i++) memset(map[i], 32, p[6]);
	print(6, 0, 0); //打表
	while(scanf("%d", &n) && n-- >= 0){
		for(int i=0; i<p[n]; i++){
				map[i][p[n]]=0;
				puts(map[i]);
				map[i][p[n]]=' ';
		}
		puts("-");
	}
	return 0;
}

二 数学方法

在discuss里面看到这个短小精悍的程序。

 

#include"stdio.h"
#include"math.h"
main()
{
	int i,j,n,ii,jj,k;
	while(scanf("%d",&n)&&n--!=-1)
	{
		for(i=0;i<pow(3,n);i++,printf("\n"))
			for(j=0;j<pow(3,n);j++)
			{
				for(ii=i,jj=j,k=0;k<n&&(ii%3+jj%3)%2==0;ii/=3,jj/=3,k++);
				printf("%c",32+56*(k==n));
			}
		printf("-\n");
	} 
}

关于这个图形,还可以用来证明

寻找1/5 + 1/25 + 1/125 + .. = 1/4的图形证明

 

 

 


 

  \

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,