博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[模拟]CODEVS-1160 蛇形矩阵
阅读量:3733 次
发布时间:2019-05-22

本文共 1021 字,大约阅读时间需要 3 分钟。

代码

#include
using namespace std;const int maxn = 200; int x, y, n, number = 1; int a[maxn][maxn]; //矩阵 void print_a(){ int line = n; for(int i = 1; i <= n; i ++) { for(int j = 1; j <= n; j ++) cout << a[i][j] << " "; cout << endl; }}void print_sum() //求对角线之和{ int sum = 0; for(int i = 1; i <= n; i ++) sum += a[i][i] + a[i][n - i + 1]; cout << sum - 1; }void fill(int circle) //填充第circle圈{ int steps = 2*circle - 2; for(int i = 0; i < steps; i ++) //上 a[x--][y] = ++ number; x++; for(int i = 0; i < steps; i ++) //左 a[x][--y] = ++ number; for(int i = 0; i < steps; i ++) //下 a[++x][y] = ++ number; for(int i = 0; i < steps; i ++) //右 a[x][++y] = ++number; y++; //y多增1,正好是下一圈要填充的点的坐标 }int main(){ int circle = 2; //从第二圈开始填充 cin >> n; x = (n + 1)/2, y = x + 1; a[x][x] = number; //中心 while(circle*2-1 <= n) { fill(circle); circle ++; } print_a(); print_sum(); }

转载地址:http://hkuin.baihongyu.com/

你可能感兴趣的文章
计算机网络知识点
查看>>
CentOS 7防火墙开放端口
查看>>
流量与日志分析
查看>>
python-端口扫描
查看>>
python实现文件上传
查看>>
Wireshark数据包分析
查看>>
Metasploit Framewok的简单使用
查看>>
Bugku-Linux2
查看>>
msf-ftp
查看>>
phpstudy设置允许远程访问mysql数据库
查看>>
msf-mysql弱口令爆破
查看>>
hydra的基本使用方法
查看>>
HCTF2018 WarmUp
查看>>
docker的基本使用方法
查看>>
Cookie重放攻击
查看>>
php中的$$变量覆盖问题
查看>>
linux上部署tomcat
查看>>
php变量覆盖
查看>>
python 后门脚本
查看>>
windows 快速生成一个填充文件
查看>>