分形幾何(遞歸)


 

Description

分形,具有以非整數維形式充填空間的形態特征。通常被定義為“一個粗糙或零碎的幾何形狀,可以分成數個部分,且每一部分都(至少近似地)是整體縮小后的形狀”,即具有自相似的性質。

一個分形塊可以定義為如下內容:

  • 度數為1的分形塊表示為:

X

  • 度數為2的分形塊表示為:

X X
 X
X X

  • 如果用B(n-1)表示度數為n-1的分形塊,那么度數為n的分形塊可以遞歸地定義為如下形式:
B(n - 1)        B(n - 1)

        B(n - 1)

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

你的任務是繪制一個度數為n的分形塊!

Input

輸入包括多個測試樣例,每個測試樣例是一個不超過7的正整數,以-1表示輸入結束。

Output

對於每一個測試樣例,用大寫字母‘X’表示分形盒子中的元素,按照度數要求輸出分形塊。每一個分形塊輸出結束后用破折號‘-’分隔。

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
-

HINT

!輸出數據中每行后面均有以空格符。注意輸出格式控制!

 

 

 

 

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <iostream>
  4 #include <string>
  5 #include <math.h>
  6 #include <algorithm>
  7 #include <vector>
  8 #include <stack>
  9 #include <queue>
 10 #include <set>
 11 #include <map>
 12 #include <sstream>
 13 const int INF=0x3f3f3f3f;
 14 typedef long long LL;
 15 const int mod=1e9+7;
 16 //const double PI=acos(-1);
 17 #define Bug cout<<"---------------------"<<endl
 18 const int maxn=1e5+10;
 19 using namespace std;
 20 
 21 void PT(int n)//輸出n個空格 
 22 {
 23     for(int i=0;i<n;i++)
 24         printf(" ");
 25 }
 26 
 27 void F(int n,int L,int flag)//n為度數,L為層數,flag為1代表左邊(要填充),flag為0代表右邊 
 28 {
 29     if(n==1)
 30         printf("X");
 31     else
 32     {
 33         if(L<=pow(3,n-1)/3)
 34         {    
 35             if(flag)
 36                 F(n-1,L,flag);
 37             else
 38                 F(n-1,L,!flag);
 39             PT(pow(3,n-2));
 40             F(n-1,L,flag);
 41         }
 42         else if(L<=2*pow(3,n-1)/3)
 43         {
 44             PT(pow(3,n-2));
 45             F(n-1,L%(int)(pow(3,n-1)/3),flag);
 46             if(flag)
 47             PT(pow(3,n-2));        
 48         }
 49         else
 50         {
 51             if(flag)
 52                 F(n-1,L%(int)(pow(3,n-1)/3),flag);
 53             else
 54                 F(n-1,L%(int)(pow(3,n-1)/3),!flag);
 55             PT(pow(3,n-2));
 56             F(n-1,L%(int)(pow(3,n-1)/3),flag);
 57         }
 58     }
 59 }
 60 
 61 void Solve(int n) 
 62 {
 63     if(n==1)
 64     {
 65         printf("X");
 66         return ;
 67     }
 68     for(int i=1;i<=pow(3,n-1)/3;i++)
 69     {
 70         F(n-1,i,1);
 71         PT(pow(3,n-2));
 72         F(n-1,i,0);
 73         printf("\n");
 74     }
 75     for(int i=1;i<=pow(3,n-1)/3;i++)
 76     {
 77         PT(pow(3,n-2));
 78         F(n-1,i,0);
 79         printf("\n");
 80     }
 81     for(int i=1;i<=pow(3,n-1)/3;i++)
 82     {
 83         F(n-1,i,1);
 84         PT(pow(3,n-2));
 85         F(n-1,i,0);
 86         printf("\n");
 87     }
 88 }
 89 
 90 int main()
 91 {
 92     int n;
 93     while(~scanf("%d",&n)&&n!=-1)
 94     {
 95         if(n==1)
 96             printf("X\n");
 97         else Solve(n);
 98         printf("-\n");
 99     }
100     return 0;
101 }

 

 

 

類似題:

有一天小李爸爸老李送了個積木給小李玩,積木里面就分兩種,黑白方塊,老李給了小李一個任務,這里給出圖形堆的規則
第一階段
1
第二階段
1 1
0 1
第三階段
1 1 1 1 
0 1 0 1
0 0 1 1
1 0 0 1

. . .等
規則是:
分為四個部分
1 | 1
——
0 | 1
左1  右1   右2   都是和 n-1階段相同
左2 和 n-1階段相反 
然后老李想要知道第n階段是什么

輸入描述:

第一行  輸入一個t,代表數據組數(1<=t<=10)
第二行 輸入一個n,代表要求第n階段是什么(1<=n<=10)

輸出描述:

下面t組數據,分別輸出第n階段的積木堆的情況
示例1

輸入

3
1
2
3

輸出

1
1 1
0 1
1 1 1 1 
0 1 0 1
0 0 1 1
1 0 0 1

 

代碼如下:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <sstream>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 const int mod=1e9+7;
16 //const double PI=acos(-1);
17 #define Bug cout<<"---------------------"<<endl
18 const int maxn=1e5+10;
19 using namespace std;
20 
21 void PT()
22 {
23     printf(" ");
24 }
25 
26 void F(int n,int L,int flag)
27 {
28     if(n==1)
29     {
30         if(flag) printf("1");
31         else printf("0");
32         return ;
33     }
34     if(L<=pow(2,n-1)/2)
35     {
36         F(n-1,L,flag);PT();F(n-1,L,flag);
37     }
38     else
39     {
40         F(n-1,L-(int)(pow(2,n-1)/2),!flag);PT();F(n-1,L-(int)(pow(2,n-1)/2),flag);
41     }
42 }
43 
44 void Solve(int n) 
45 {
46     for(int i=1;i<=pow(2,n-1)/2;i++)
47     {
48         F(n-1,i,1);PT();F(n-1,i,1);
49         printf("\n");
50     }
51     for(int i=1;i<=pow(2,n-1)/2;i++)
52     {
53         F(n-1,i,0);PT();F(n-1,i,1);
54         printf("\n");
55     }
56 }
57 
58 int main()
59 {
60     int T;
61     scanf("%d",&T);
62     while(T--)
63     {
64         int n;
65         scanf("%d",&n);
66         if(n==1)
67             printf("1\n");
68         else Solve(n);
69     }
70 }

 

隊友用string寫的

 1 //MADE BY Y_is_sunshine;
 2 //#include <bits/stdc++.h>
 3 //#include <memory.h>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <cstdlib>
 7 #include <cstring>
 8 #include <sstream>
 9 #include <cstdio>
10 #include <vector>
11 #include <string>
12 #include <cmath>
13 #include <queue>
14 #include <stack>
15 #include <map>
16 #include <set>
17  
18 #define INF 0x3f3f3f3f
19 #define MAXN 200005
20  
21 typedef long long ll;
22  
23 const int mod = 998244353;
24 const double PI = acos(-1);
25  
26 using namespace std;
27  
28 int N, M, K;
29  
30 int main(void)
31 {
32     //freopen("data.txt", "r", stdin);
33  
34     ios_base::sync_with_stdio(false);
35     cin.tie(NULL);
36  
37     //cout << (2 << 9) << endl;
38  
39     int T;
40     cin >> T;
41     while (T--) {
42         string s[1050];
43         int cnt = 1;
44         s[1] = "1";
45         cin >> N;
46         while (--N) {
47             for (int i = 1; i <= cnt; i++) {
48                 //s[i + cnt] = s[i], s[i + cnt].reserve();
49                 for (auto it1 : s[i])
50                     s[i + cnt] += it1 == '1' ? '0' : '1';
51             }
52             for (int i = 1; i <= cnt; i++)
53                 s[i + cnt] += s[i];
54             for (int i = 1; i <= cnt; i++)
55                 s[i] += s[i];
56             cnt *= 2;
57         }
58          
59         /*for (int i = 1; i <= cnt; i++)
60             cout << s[i] << '\n';*/
61         for (int i = 1; i <= cnt; i++) {
62             for (int j = 0; j < s[i].size(); j++)
63                 cout << s[i][j] << (j == s[i].size() - 1 ? '\n' : ' ');
64         }
65  
66     }
67      
68  
69  
70  
71     //freopen("CON", "r", stdin);
72     //system("pause");
73     return 0;
74 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM