分形(哈尔滨理工大学校赛)
如今回想上学期犯了很多错误,如今再次发这篇博客就是因为这些错误。
所以这学期一定要把遇到的问题都解决,不要把问题都留到已经忘了,同时也要时常看看曾经做过的题,复习做过的,继续做曾经没做的。
然后就是这道题了。帮助我理解分形的一道题。注释和想说的全在代码里了。
Help C5 |
||||||
|
||||||
Description |
||||||
Hello, I’m Sea5, and you can call me C5 instead. I want a program which can sign my name automatically. And my brothers, C0, C1, C2, C3, C4, C6, C7, C8, each of them wants one as well. Can you help us? |
||||||
Input |
||||||
First line is the number of test cases T(T<=8). T lines follow, each line includes an integer N(N<=7), and you should help C(N) to sign his name. |
||||||
Output |
||||||
C0’s signature is ‘C’. When you draw C(N)’s name, you should print the name using C(N-1)’s name as its element, and using the following format to draw it. *XX X** *XX (X is the element, * is blank space) And please don’t print extra spaces at the end of line. For example, C1’s name should be *CC *CC C C** *CC But not *CC (I use * to show you where are spaces.) |
||||||
Sample Input |
||||||
3 |
||||||
Sample Output |
||||||
C |
1 ///分形打印的过程中点的辐射成多个点的相加过程是与实际相反的, 2 ///先加的是正常最后一次辐射的大小,然后最后相加的是第一次辐射的大小, 3 ///由于最后加法的交换行只要最后结果, 4 ///所以最后是一样的,但如果保留过程就会错误 5 ///这题是一道很好的模板。源自哈尔滨理工大学校赛 6 #include<cstdio> //分形问题递归打印解决 7 8 #include<cstring> 9 10 #include<iostream> 11 #include<windows.h> 12 using namespace std; 13 14 15 16 char a[3000][3000]; //注意数组的大小 不然存不下图形 会RE 17 18 int mypow(int d) 19 20 { 21 22 int ans = 1; 23 24 for (int i = 1; i <= d; i++) 25 26 ans *= 3; 27 28 return ans; 29 30 } 31 32 void dfs(int cur, int x, int y) 33 34 { 35 36 if (cur == 1) 37 38 { 39 40 a[x][y] = 'C'; 41 42 return ; 43 44 } 45 46 int s = mypow(cur - 2); 47 48 dfs(cur - 1, x + s, y); //这些按照格式来就好 49 dfs(cur - 1, x, y + 2 * s); 50 51 dfs(cur - 1, x, y + s); 52 53 dfs(cur - 1, x + 2 * s, y + s); 54 dfs(cur - 1, x + 2 * s, y + 2 * s); 55 56 } 57 58 int main(void) 59 60 { 61 62 int t; 63 64 scanf("%d", &t); 65 66 while (t--) 67 68 { 69 70 int n; 71 72 scanf("%d", &n); 73 74 n++; 75 76 memset(a, ' ', sizeof(a)); 77 78 dfs(n, 1, 1); 79 80 int s = mypow(n - 1);///此为最大边界 81 82 for (int i = 0; i <= s; i++) //先把最大的边界找到 83 84 a[i][s + 1] = '\0'; 85 ///次下为找到边界的方法,可以当做模板 86 for (int i = 0; i <= s; i++) //其实格式可以每行从后往前来把每个编程\0遇到不是空格就停止 87 88 { 89 90 for (int j = s + 1; j >= 0; j--) 91 92 { 93 94 if (a[i][j] == 'C') { 95 a[i][j + 1] = '\0'; 96 break; 97 } 98 99 } 100 101 } 102 103 for (int i = 1; i <= s; i++) 104 105 printf("%s\n", a[i] + 1); //a[i]+1 就是从a[1]开始输出 106 107 } 108 109 return 0; 110 111 }