蛇形填数(矩阵)

#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;

}

相关推荐
虾球xz16 分钟前
CppCon 2018 学习:THE MOST VALUABLE VALUES
开发语言·c++·学习
2401_881244402 小时前
牛客周赛99
c++
山登绝顶我为峰 3(^v^)35 小时前
如何录制带备注的演示文稿(LaTex Beamer + Pympress)
c++·线性代数·算法·计算机·密码学·音视频·latex
十五年专注C++开发8 小时前
CMake基础:条件判断详解
c++·跨平台·cmake·自动化编译
QuantumStack10 小时前
【C++ 真题】P1104 生日
开发语言·c++·算法
天若有情67310 小时前
01_软件卓越之道:功能性与需求满足
c++·软件工程·软件
whoarethenext10 小时前
使用 C++/OpenCV 和 MFCC 构建双重认证智能门禁系统
开发语言·c++·opencv·mfcc
Jay_51511 小时前
C++多态与虚函数详解:从入门到精通
开发语言·c++
xiaolang_8616_wjl12 小时前
c++文字游戏_闯关打怪
开发语言·数据结构·c++·算法·c++20
FrostedLotus·霜莲13 小时前
C++主流编辑器特点比较
开发语言·c++·编辑器