Tsinghua machine test - stacking basket problem

Title description:

Stack baskets with a size difference of one garden on top of each other so that when viewed from above, the colors of the side baskets are staggered. Now let the computer complete this work, it depends on you.

enter:

The input is a triplet. They are: the size n of the outer basket (n is an odd integer satisfying 0<n<80), the character of the center suit and the character of the outer basket, and the latter two are ASCII visible characters.

output:

Output the pattern of stacked baskets, the center suit and outer basket suits are staggered and stacked from the inner layer. When multiple baskets are stacked, the corners of the outermost basket are always broken and polished. There should be a row of gaps between stacked baskets.

Sample input:

11 B A

5 @ W

Sample output:

AAAAAAAAA

ABBBBBBBBBA

ABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

ABBBBBBBBA

FATHERS

ABABABABABA

FATHERS

ABBBBBBBBA

ABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

ABBBBBBBBBA

AAAAAAAAA

@@@

@WWW@

@W@W@

@WWW@

@@@

topic analysis

Use the array padding method:

  • Allocate fixed-size arrays ahead of time
  • Treat a circle of filling as a cycle, determine the starting position of each cycle, that is, the upper left coordinates (loc, loc), and the length of each circle length
  • Starting from the center, the array is filled from the inside to the outside
  • Remember, there is no padding in the four corners, remove it in the array, or avoid it when printing
#include <stdio.h>
//叠框问题
int main(){
    
    
    int n;
    char a, b;//a在内
    while(scanf("%d %c %c", &n, &a, &b) != EOF){
    
      //输入个数未知,用循环接受
        printf("n=%d,a=%c,b=%c\n", n, a, b);
        char pic[80][80] = {
    
    0}; //每一个元素都被初始化为\0
        int flag = 1; //控制字符为a或b,初始为a,每层交替变换
        char ch;
        for(int length = 1, loc = n/2;length <= n;length += 2, loc--){
    
    //更新每一圈的起始位置(loc, loc)和长度
            if(flag == 1){
    
    
                ch = a;
            }else{
    
    
                ch = b;
            }
            for (int i = 0; i < length; i++) {
    
    //上下边框
                pic[loc][loc+i] = ch;
                pic[loc+length-1][loc+i] = ch;
            }
            for (int i = 1; i < length - 1; i++){
    
    //左右边框
                pic[loc+i][loc] = ch;
                pic[loc+i][loc+length-1] = ch;
            }
            flag = flag*(-1); //改变符号标志
        }
        for (int i = 0; i < n; i++) {
    
    
            for (int j = 0; j < n; j++) {
    
    
                if((n!=1)&&(((i == 0)&&(j == 0)) || ((i == n-1)&&(j == 0)) || ((i == 0)&&(j ==n-1)) || ((i ==n-1)&&(j == n-1)))){
    
     //四个角不进行输出
                    printf(" ");
                }else{
    
    
                    printf("%c", pic[i][j]);
                }
            }
            printf("\n");
        }
    }
    return 0;
}

Result display

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/gary101818/article/details/128928085