DHU OJ 二维数组 n 层正方形

 思路及代码

// n个数 s = 2n-1
/*
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
*/
//num1 row(1,1)->(1,5),(5,1)->(5,5);column(1,1)->(5,1),(1,5)->(5,5)
//num2 row(2,2)->(2,4),(4.2)->(4,4);column(2,2)->(4,2),(2,4)->(4,4)
//num3 row(3,3)->(3,3);column(3,3)->(3,3);
// s 层
//只管前 n 行,后面复制
//num1 row(1,1)->(1,n) column(1,1)->(1,s),(n,1)->(n,s);
//num2 row(2,2)->(2,n-1) column(2,2)->(2,s),(n-1,2)->(n-1,s);
//num n row(n,n)->(n,s-n+1) column(n,n)->(n,n)
//num k row(k,k)->(k,s-k+1) column(k,k)->(n,k),(k,s-k+1)->(n,s-k+1)
//下标 规律-1,规律方便理解
//复制剩余行
//input n 1<= <=25
#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
    int n;
    cin >> n;
//solution
    int s = 2*n - 1;
    int square[s][s];
//前 n 行
//from 1->n
    for (int num = 1; num <= n; num++){
        for (int i = num-1; i <= s-num;i++){
            square[num-1][i] = num;
        }
        for (int j = num-1; j <= n-1; j++){
            square[j][num-1] = num;
            square[j][s-num] = num;
        }
    }
//剩余行
//from 1->n
    for (int num = 1; num <= n; num++){
        memcpy(square[s-num], square[num-1],sizeof(int)*s);
    }
//output
    for (int x = 0; x <= s-1;x++){
        for (int y = 0; y <= s-1; y++){
            cout << square[x][y] << ' ';
            
        }
        cout << endl;
    }
    return 0;
}

感觉整个人都不好了

参考:1️⃣C++ 多维数组 | 菜鸟教程

2️⃣ https://wenku.csdn.net/answer/443qxau0jp

3️⃣二维数组的初始化,下标,遍历,及数组间的赋值_二维数组下标-CSDN博客

收获:1️⃣复习二维数组的初始化,遍历,访问元素直接访问

2️⃣

void *memcpy(void *to, const void *from, size_t numBytes);
//to: A pointer to the memory location where the copied data will be stored.
//from: A pointer to the memory location from where the data is to be copied.
//numBytes: The number of bytes to be copied.

菜菜,不是教程,做题和学习记录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值