蛇形填数(矩阵)

#include <bits/stdc++.h>

using namespace std;

int main() {

int a[10][10] = {0};

int n;

cin >> n;

int num = 1;

int bot = n - 1;

int top = 0;

int left = 0;

int right = n - 1;

while (n * n >= num) {

for (int i = top; i <= bot; i++) {

a[i][right] = num++;

}

right--;

if (top <= bot) {

for (int j = right; j >= left; j--) {

a[bot][j] = num++;

}

bot--;

}

if (right >= left) {

for (int i = bot; i >= top; i--) {

a[i][left] = num++;

}

left++;

}

for (int j = left; j <= right; j++) {

a[top][j] = num++;

}

top++;

}

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

cout << setw(4) << a[i][j];

}

cout << endl;

}

return 0;

}

相关推荐
楼田莉子32 分钟前
Linux学习:线程的同步与互斥
linux·运维·c++·学习
liulun1 小时前
C++ WinRT中的异步
c++·windows
王老师青少年编程2 小时前
2020年信奥赛C++提高组csp-s初赛真题及答案解析(阅读程序第1题)
c++·题解·真题·初赛·信奥赛·csp-s·提高组
会周易的程序员2 小时前
cNetgate插件架构设计详解 动态库 脚本二开lua, python, javascript
javascript·c++·python·物联网·lua·iot
普通网友2 小时前
C++与Rust交互编程
开发语言·c++·算法
xyty33203 小时前
QImageReader 的全局静态锁原理
c++·windows·qt
散峰而望3 小时前
【算法竞赛】堆和 priority_queue
开发语言·数据结构·c++·算法·贪心算法·动态规划·推荐算法
adore.9683 小时前
2.20 oj83+84+85
c++·复试上机
alexwang2113 小时前
B2007 A + B 问题 题解
c++·算法·题解·洛谷