AcWing 118. 分形

传送门

#include <bits/stdc++.h>

using namespace std;
int ans[3010][3010];

int main() {
    int n;
    while (cin >> n) {
        if (n == -1)
            break;
        ans[1][1] = 1;
        int len = pow(3.0, n - 1);
        for (int k = 1; k < len; k *= 3) {
            for (int i = 1; i <= k; i++) {
                for (int j = 1; j <= k; j++) {
                    ans[i + 2 * k][j] = ans[i][j + 2 * k] = ans[i][j];
                    ans[i + k][j + k] = ans[i + 2 * k][j + 2 * k] = ans[i][j];
                }
            }
        }
        for (int i = 1; i <= len; i++) {
            for (int j = 1; j <= len; j++) {
                if (ans[i][j])
                    cout << "X";
                else
                    cout << " ";
            }
            cout << endl;
        }
        puts("-");
    }
    return 0;
}
上一篇:linux find rm ls 逻辑非运用


下一篇:LeetCode(#118)————杨辉三角形