POJ 2083 Fractal
打印图形,使用二维数组graph[][]来存储图形,width[]存储图形的宽度。
G(n) 组成图案是: G(n-1) G(n-1) G(n-1) G(n-1) G(n-1)
可以运用递归的先打印出整个图案,宽度为width[7], 当输入n时,选择性输出图案。
#include <stdio.h>#include <string.h>#define N 800char graph[N][N];int width[] = {0,1,3,9,27,81,243,729};//draw the graphvoid draw(int n, int x, int y) { if(n == 1) { graph[x][y] = 'X'; return ; } int w = width[n-1]; draw(n-1, x, y); draw(n-1, x, y+2*w); draw(n-1, x+w, y+w); draw(n-1, x+2*w, y); draw(n-1, x+2*w, y+2*w);}int main() { memset(graph, ' ', sizeof(graph)); draw(7, 0, 0); int n; while(scanf("%d", &n)!=EOF){ if(n == -1) break; int w = width[n]; for(int i = 0; i < w; i++){ for(int j = 0; j < w; j++) putchar(graph[i][j]); putchar('/n'); } puts("-"); } return 0;}新闻热点
疑难解答