Typing universe recursive solution (1296)

Subject description:

Scientists magical world of fractal cosmology that the universe is a fundamental particle, composed of countless particles of the universe and there will be other small universe. Fractal defined as follows:

1 degree to fractal:

X

2 is of fractal:

X X
 X
X X

If B (n-1) n-1 represents the degree of the fractal, the fractal recursion of n is defined as follows:

B(n-1) B(n-1)
   B(n-1)
B(n-1) B(n-1)

Fractal requirements of a given degree, the output of the corresponding fractal FIG.

Enter a description:

Plural sets of inputs, each input a positive integer n

Output Description:

Corresponding to the number of each group, the corresponding output points chart, and with a "-" separator

Sample input:

1

2

Sample output:

X
-
X X
 X
X X
-

 

 1 #include<iostream>
 2 #include<math.h>
 3 using namespace std;
 4 char m[2000][2000];
 5 void d(int n, int x, int y);
 6 int main(void){
 7     int n;
 8     while(cin>>n){
 9         int size = pow(3.0, n-1);
10         for(int i = 0; i <= size; i++){
11             for(int j = 0; j <= size; j++){
12                 m[i][j] = ' ';
13             }
14         }
15         d(n, 1, 1);
16         for(int i = 1; i <= size; i++){
17             for(int j = 1; j <= size; j++){
18                 cout<<m[i][j];
19             }
20             cout<<endl;
21         }
22         cout<<"-"<<endl;
23     }
24     return 0;
25 }
26 void d(int n, int x, int y){
27     if(n == 1){
28         m[x][y] = 'X';
29     }
30     else{
31         int size = pow(3.0, n-2);
32         d(n-1, x, y);
33         d(n-1, x, y+2*size);
34         d(n-1, x+size, y+size);
35         d(n-1, x+2*size, y);
36         d(n-1, x+2*size, y+2*size);
37     }
38 }
signal

 

Guess you like

Origin www.cnblogs.com/zq-dmhy/p/11074356.html