蓝桥PREV-2_打印十字架_二维数组的使用

蓝桥杯考前训练题2
打印十字架

心得体会 :在写题之前 切记需求分析
完整分析之后再写,能够得到正确的答案

还有就是 使用c++ new一个二维数组的过程
可以记得写

 char **f = new char *[sum/2+2];
    for(int i = 0; i < sum/2+2; i++)
        f[i] = new char [sum];

通过这种定义一个二级指针(**)的方法来达到定义一个动态的二维数组

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
/**
1  ..$$$$$$$$$$$$$..//3
2  ..$...........$..//3
3  $$$.$$$$$$$$$.$$$//3,2
4  $...$.......$...$//3,2
5  $.$$$.$$$$$.$$$.$//3,2,1
6  $.$...$...$...$.$//3,2,1
7  $.$.$$$.$.$$$.$.$//3,2,1,0
8  $.$.$...$...$.$.$//3,2,1,0
9  $.$.$.$$$$$.$.$.$//3,2,1,0
   $.$.$...$...$.$.$//3,2,1,0
   $.$.$$$.$.$$$.$.$//3,2,1,0
   $.$...$...$...$.$//3,2,1
   $.$$$.$$$$$.$$$.$//3,2,1
   $...$.......$...$//3,2
   $$$.$$$$$$$$$.$$$//3,2
   ..$...........$..//3
   ..$$$$$$$$$$$$$..//3

//第一层 宽5 高5
每增高一层上加二 下加二
宽度也是加二
例如 第三层:宽 5+(2*3)*2
如果只记录四分之一个十字架 就是 2+(2*3)

*/

using namespace std;

int main()
{
    int n;//用来记录有多少圈
    scanf("%d",&n);
    int sum = 5+(2*n*2);//记录有多少层,宽度也是相同的

    char **f = new char *[sum/2+2];
    for(int i = 0; i < sum/2+2; i++)
        f[i] = new char [sum];
    int tc1=2+2*n;
    int tc2=2+2*n;
    for(int i = 0; i<sum/2+1; i++)
    {
        char *a;
        a = new char[sum];
        for(int j=0; j<sum; j++)
            a[j]='.';//每一层都预先设置为*;
        int temp = 2+2*n;//计算中心线
        int temp2 = 2*(n-i/2);//计算边缘线
        if(i%2==0)
        {
            a[temp]='$';//中心线打印为$;
            for(int t=temp-temp2; t<=temp+temp2; t++)
                a[t]='$';
            if(i/2>0)
                if(temp-temp2-1>0)
                {
                    a[tc1]=a[tc2]='$';
                    a[temp-temp2-4]=a[temp-temp2-2]=a[temp-temp2-3]=a[temp+temp2+2]=a[temp+temp2+3]=a[temp+temp2+4]='$';
                    tc1 = temp-temp2-4;
                    tc2 = temp+temp2+4;
                }
            for(int t = temp-temp2-4; t>=0; t=t-2)
                a[t]='$';
            for(int t = temp+temp2+4; t<=sum; t=t+2)
                a[t]='$';

        }
        if(i%2!=0)
        {
            if(tc1!=2+2*n)
                a[tc1]=a[tc2]='$';
            a[temp-temp2]=a[temp+temp2]='$';
            if(i>4)
            {
                for(int t=tc1; t>=0; t=t-2)
                    a[t]='$';
                for(int t=tc2; t<=sum; t=t+2)
                    a[t]='$';
            }
        }

        for(int j=0; j<sum; j++)
        {
            cout <<a[j];
            f[i][j]=a[j];
        }
        cout <<endl;
    }
    for(int i = sum/2-1; i >=0; i--)
    {
        for(int j=0; j<sum; j++)
            cout << f[i][j];
            cout << endl;
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值