当前位置:网站首页>[Jiudu OJ 07] folding basket

[Jiudu OJ 07] folding basket

2022-07-06 05:37:00 CodeLinghu

【 Nine degrees OJ 07】 Folding basket

One 、 The question

Two 、 The answer process

Ideas :

First define a two-dimensional array , Then fill in characters from inside to outside , Finally, remove the four corners .

Note:

  • The coordinates of the characters in the upper left corner of the middle are : (n/2+1,n/2+1)
  • The coordinates of the upper left corner of the secondary middle are : (n/2+1-1,n/2+1-1)
#include<stdio.h>
int main(){
    
    int outputbuf[82][82];// Output cache for pre typesetting 
    char a,b;// Two characters entered 
    int n;// Stack basket size 
    bool firstcase=true;// Whether it is the first group of data marks , The initial value is true
    while(scanf("%d %c %c",&n,&a,&b)==3){
    //scanf() The return value of the function is  : Number of data items successfully assigned 
        if(firstcase==true){
    // If the first set of data 
            firstcase=false;// Mark the first set of data as false
        }
        else printf("\n");

        for(int i=1,j=1;i<=n;i+=2,j++){
    // Output each circle from the inside out 
            int x=n/2+1,y=x;//
            x-=j-1;y-=j-1;// Calculate the coordinates of the upper right corner of each circle 
            char c=j%2==1 ?a:b;// Calculate which character the current circle needs to use 
            for(int k=1;k<=i;k++){
     // On the top of the current circle 、 Next 、 Left 、 Right assignment 
                outputbuf[x+k-1][y]=c;// Left assignment 
                outputbuf[x][y+k-1]=c;// Upper assignment 
                outputbuf[x+i-1][y+k-1]=c;// Right assignment 
                outputbuf[x+k-1][y+i-1]=c;// Lower assignment 
            }
        }
        if(n!=1){
    // Set the four corners to empty 
            outputbuf[1][1]=' ';
            outputbuf[n][1]=' ';
            outputbuf[1][n]=' ';
            outputbuf[n][n]=' ';
        }
        for(int i=1;i<=n;i++){
    
            for(int j=1;j<=n;j++){
    
                printf("%c",outputbuf[i][j]);
            }
            printf("\n");
        }
    }
}
原网站

版权声明
本文为[CodeLinghu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202132049488158.html