- C++
分形 盒
- 2024-3-10 19:28:49 @
2 条评论
-
admin SU @ 2024-3-10 19:30:41
-
2024-3-10 19:30:30@
#include <cmath> #include <iostream> using namespace std; //7度盒分形 最大规模n=3^(7-1)=729 #define MAX 730 char maps[MAX][MAX]; void printBox(int n, int x, int y) { //递归边界 if(n == 1){ maps[x][y] = 'X'; } else{ //n-1度盒分形的规模m int m = pow(3, n - 2); //左上方的n-1度盒分形 printBox(n - 1, x, y); //右上方的n-1度盒分形 printBox(n-1, x+2*m, y); //中间的n-1度盒分形 printBox(n - 1, x , y + 2 * m); //左下方的n-1度盒分形 printBox(n - 1, x + m, y + m); //右下方的n-1度盒分形 printBox(n-1,x+2*m,y+2*m); } } int main() { int n ; cin >> n; while (n != -1){ int size = pow(3, n - 1); //初始化 for (int i = 0; i < size; i++){ for (int j = 0; j < size; j++){ maps[i][j] = ' '; maps[i][size] = '\0'; } } printBox(n, 0, 0); //输出 for (int i = 0; i < size; i++) printf("%s\n", maps[i]); cout << "-"<<endl; cin >> n; } return 0; }
- 1