思路及代码
// 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.
菜菜,不是教程,做题和学习记录
标签:OJ,column,二维,正方形,int,num,数组,DHU,row From: https://blog.csdn.net/jtymyxmz/article/details/141140561