蛇形填数(矩阵)

#include <bits/stdc++.h>

using namespace std;

int main() {

int a1010 = {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++) {

airight = num++;

}

right--;

if (top <= bot) {

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

abotj = num++;

}

bot--;

}

if (right >= left) {

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

aileft = num++;

}

left++;

}

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

atopj = num++;

}

top++;

}

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

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

cout << setw(4) << aij;

}

cout << endl;

}

return 0;

}

相关推荐
无限的鲜花6 小时前
协程本质是函数加状态机——零基础深入浅出 C++20 协程
c++·算法·c++20
精明的身影6 小时前
C++自学之路1:Hello world
开发语言·c++
旖-旎6 小时前
《LeetCode 64 最小路径和 || LeetCode 174 地下城游戏》
c++·算法·leetcode·动态规划
森林古猿16 小时前
再论斜率优化
c++·学习·算法
水利行业RTU手艺人7 小时前
RTU固件中的“数据保险箱”——离线数据补发系统设计
c++·stm32·单片机·物联网
凯瑟琳.奥古斯特7 小时前
力扣1009补码解法C++实现
开发语言·c++·算法·leetcode·职场和发展
zh_xuan10 小时前
c++ optional用法
开发语言·c++·optional
Let's Chat Coding11 小时前
身份识别、身份认证与授权的区别
c++
鹏易灵12 小时前
C++——8.移动语义初探(移动构造、移动赋值)
开发语言·c++·php
凯瑟琳.奥古斯特12 小时前
力扣1008:前序重建BST
开发语言·c++·算法·leetcode·职场和发展