程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 有趣的分形圖形-遞歸和數學方法解決-POJ 2083

有趣的分形圖形-遞歸和數學方法解決-POJ 2083

編輯:C++入門知識

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的圖形證明

 

 

 


 

  \

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved