#include <stdio.h> #include <stdlib.h> #include <string.h>
char *arr; int len, n;
void draw_h(int x, int y, int hlen) { int p = y * len + x; while (hlen--) arr[p++] = '$'; }
void draw_v(int x, int y, int hlen) { int p = y * len + x; while (hlen--) { arr[p] = '$'; p += len; } }
void draw(int layer) { int xl = layer * 2; int xr = len - 1 - xl; int yt = (layer - 1) * 2; int yb = len - 1 - (layer - 1) * 2; int border = 5 + (n - layer) * 4; // 图案第一行(以及倒数第一行) draw_h(xl, yt, border); draw_h(xl, yb, border); // 图案第二行 (以及倒数第二行) draw_h(xl, yt + 1, 1); draw_h(xr, yt + 1, 1); draw_h(xl, yb - 1, 1); draw_h(xr, yb - 1, 1); // 图案第三行(以及倒数第三行) draw_h(xl - 2, yt + 2, 3); draw_h(xr, yt + 2, 3); draw_h(xl - 2, yb - 2, 3); draw_h(xr, yb - 2, 3); // 两边的竖线 draw_v(xl - 2, yt + 2, border); draw_v(xr + 2, yt + 2, border); }
int main(void) { int x, y, size; char *pArr; scanf("%d", &n); len = 5 + 4 * n; size = len * len * sizeof(char); pArr = arr = (char *)malloc(size); // 画每一层图案(不是每一行) memset(arr, '.', size); for (x = 1; x <= n; x++) draw(x); // 画中心十字 draw_v(2 * n + 2, len / 2 - 2, 5); draw_h(2 * n, len / 2, 5); for (y = 0; y < len; y++) { for (x = 0; x < len; x++) putchar(*pArr++); putchar('\n'); } free(arr); return 0; }
|