OJ刷题之旅

题目

现在给你两种颜色的箩筐,需要的时候,就把一个个大小差一圈的筐叠上去,使得从上往下看时,边筐花色交错。这个工作现在要让计算机来完成,得看你的了。

输入

输入是一个个的三元组,分别是,外筐尺寸n(n为满足0

输出

输出叠在一起的筐图案,中心花色与外筐花色字符从内层起交错相叠,多筐相叠时,最外筐的角总是被打磨掉。叠筐与叠筐之间应有一行间隔。

样例输入

11 B A 5 @ W

样例输出

AAAAAAAAA ABBBBBBBBBA ABAAAAAAABA ABABBBBBABA ABABAAABABA ABABABABABA ABABAAABABA ABABBBBBABA ABAAAAAAABA ABBBBBBBBBA AAAAAAAAA @@@ @WWW@ @W@W@ @WWW@ @@@

以下是一个满足要求的C语言程序:

#include 
#include 

int main() {
    int n; char c1, c2;
    while (scanf("%d %c %c", &n, &c1, &c2) == 3) {
        char basket[n][n];
        memset(basket, c2, sizeof(basket)); // fill the entire basket with outer color

        for (int i = 0; i < (n+1)/2; i++) { // build each layer of the basket
            char layer_c = (i % 2 == 0) ? c1 : c2; // determine center or outer color for this layer
            int layer_size = n - 2 * i;
            int start = i;
            int end = n - 1 - i;

            // fill top and bottom rows of the layer with outer color
            for (int j = start; j <= end; j++) {
                basket[start][j] = c2;
                basket[end][j] = c2;
            }

            // fill left and right columns of the layer with outer color
            for (int j = start+1; j < end; j++) {
                basket[j][start] = c2;
                basket[j][end] = c2;
            }

            // fill the center of the layer with center color
            for (int j = start+1; j < end; j += 2) {
                for (int k = start+1; k < end; k += 2) {
                    basket[j][k] = layer_c;
                }
            }
        }

        // print the basket
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                printf("%c", basket[i][j]);
            }
            printf("\n");
        }
        printf("\n");
    }

    return 0;
}

该程序的思路是构建一个二维字符数组,用于表示篮筐的图案。首先将整个篮筐填充为外部颜色,然后逐层填充中心和外部颜色的格子,最后输出整个篮筐的图案。

你可能感兴趣的:(OJ刷题之旅,c语言)