题目
标题:打印图形
如下的程序会在控制台绘制分形图(就是整体与局部自相似的图形)。
当n=1,2,3的时候,输出如下:
请仔细分析程序,并填写划线部分缺少的代码。
n=1时:
o
ooo
o
n=2时:
o
ooo
o
o o o
ooooooooo
o o o
o
ooo
o
n=3时:
o
ooo
o
o o o
ooooooooo
o o o
o
ooo
o
o o o
ooo ooo ooo
o o o
o o o o o o o o o
ooooooooooooooooooooooooooo
o o o o o o o o o
o o o
ooo ooo ooo
o o o
o
ooo
o
o o o
ooooooooo
o o o
o
ooo
o
源程序:
#include <stdio.h>
#include <stdlib.h>
void show(char* buf, int w){
int i,j;
for(i=0; i<w; i++){
for(j=0; j<w; j++){
printf("%c", buf[i*w+j]==0? ‘ ‘ : ‘o‘);
}
printf("\n");
}
}
void draw(char* buf, int w, int x, int y, int size){
if(size==1){
buf[y*w+x] = 1;
return;
}
int n = _________________________ ; //填空
draw(buf, w, x, y, n);
draw(buf, w, x-n, y ,n);
draw(buf, w, x+n, y ,n);
draw(buf, w, x, y-n ,n);
draw(buf, w, x, y+n ,n);
}
int main()
{
int N = 3;
int t = 1;
int i;
for(i=0; i<N; i++) t *= 3;
char* buf = (char*)malloc(t*t);
for(i=0; i<t*t; i++) buf[i] = 0;
draw(buf, t, t/2, t/2, t);
show(buf, t);
free(buf);
return 0;
}
注意:只提交划线部分缺少的代码,不要抄写任何已经存在的代码或符号。
代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 void show(char* buf, int w){ 5 int i,j; 6 for(i=0; i<w; i++){ 7 for(j=0; j<w; j++){ 8 printf("%c", buf[i*w+j]==0? ‘ ‘ : ‘o‘); 9 } 10 printf("\n"); 11 } 12 } 13 14 void draw(char* buf, int w, int x, int y, int size){ 15 if(size==1){ 16 buf[y*w+x] = 1; 17 return; 18 } 19 20 int n = size/3 ; //填空 21 draw(buf, w, x, y, n); 22 draw(buf, w, x-n, y ,n); 23 draw(buf, w, x+n, y ,n); 24 draw(buf, w, x, y-n ,n); 25 draw(buf, w, x, y+n ,n); 26 } 27 28 int main() 29 { 30 int N = 3; 31 int t = 1; 32 int i; 33 for(i=0; i<N; i++) t *= 3; 34 35 char* buf = (char*)malloc(t*t); 36 for(i=0; i<t*t; i++) buf[i] = 0; 37 38 draw(buf, t, t/2, t/2, t); 39 show(buf, t); 40 free(buf); 41 42 return 0; 43 }
原文:https://www.cnblogs.com/memocean/p/12292353.html