【九度 OJ 07】叠筐

简介: 【九度 OJ 07】叠筐

一、题意

二、解答过程

思路:

先定义二维数组,再由内到外一圈一圈填充字符,最后去掉四个角。

Note:

  • 中间左上角字符坐标为: (n/2+1,n/2+1)
  • 次中间左上角坐标为: (n/2+1-1,n/2+1-1)
#include<stdio.h>
int main(){
    int outputbuf[82][82];//用于预排版的输出缓存
    char a,b;//输入的两个字符
    int n;//叠筐大小
    bool firstcase=true;//是否为第一组数据标志,初始值为true
    while(scanf("%d %c %c",&n,&a,&b)==3){//scanf()函数返回值是 :成功赋值的数据项数
        if(firstcase==true){//若是第一组数据
            firstcase=false;//将第一组数据标记成false
        }
        else printf("\n");
        for(int i=1,j=1;i<=n;i+=2,j++){//从里到外输出每个圈
            int x=n/2+1,y=x;//
            x-=j-1;y-=j-1;//计算每个圈右上角点的坐标
            char c=j%2==1 ?a:b;//计算当前圈需要使用哪个字符
            for(int k=1;k<=i;k++){ //对当前圈的上、下、左、右边赋值
                outputbuf[x+k-1][y]=c;//左边赋值
                outputbuf[x][y+k-1]=c;//上边赋值
                outputbuf[x+i-1][y+k-1]=c;//右边赋值
                outputbuf[x+k-1][y+i-1]=c;//下边赋值
            }
        }
        if(n!=1){//将四角置位空
            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");
        }
    }
}


目录
相关文章
|
5月前
|
C++
C++:OJ练习(每日练习系列)
C++:OJ练习(每日练习系列)
66 2
|
5月前
二叉树oj题集(LeetCode)
二叉树oj题集(LeetCode)
|
5月前
|
Shell C++
C++:OJ练习(每日练习系列)
C++:OJ练习(每日练习系列)
47 1
|
5月前
|
Serverless C++
C++:OJ练习(每日练习!)
C++:OJ练习(每日练习!)
56 0
|
4月前
|
Serverless
每日OJ刷题
每日OJ刷题
24 1
|
5月前
|
API
Leetcode-二叉树oj题
Leetcode-二叉树oj题
28 0
Leetcode-二叉树oj题
|
5月前
LeetCode-二叉树OJ题
LeetCode-二叉树OJ题
31 0
|
5月前
二叉树OJ题目(2)
二叉树OJ题目(2)
29 0
|
5月前
|
存储
栈与队列相关OJ题
栈与队列相关OJ题
21 0
|
10月前
LeetCode——OJ题之二叉树【上】
LeetCode——OJ题之二叉树【上】
44 1