Blue Bridge Cup 2018- provincial tournament -C / C ++ - A set of 5 questions

topic

Title: Print Graphics

The following procedures are drawn in FIG console fractal (self-similarity is global and local graphics).

When n = 1,2,3, when the output is as follows:
Please be careful analysis program, and fill in the missing codes underlined.

When. 1 = n-:
  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         
          power        
           of the o         
              o            
             ooo           
              o            
     o o o   
    ooo ooo ooo  
     o o o   
  o o o o o o o o o
wipers
  o o o o o o o o o
     o o o   
    ooo ooo ooo  
     o o o   
              o            
             ooo           
              o            
           o o o         
          power        
           of the o         
              o            
             ooo           
              o            

Source:


#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;
}


Note: The underlined part is missing submit only code, not copying any existing code or symbol.

Code

 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 }

 

Guess you like

Origin www.cnblogs.com/memocean/p/12292353.html