当前位置:网站首页>2022河南萌新联赛第(五)场:信息工程大学 K - 矩阵生成
2022河南萌新联赛第(五)场:信息工程大学 K - 矩阵生成
2022-08-10 05:46:00 【WA_自动机】
K - 矩阵生成
P2615 [NOIP2015 提高组] 神奇的幻方
首先开一组循环输入。然后将第一行中间的数赋值为1,定义一个x,y表示当前位置,把x赋值为1,y赋值为(n+1)/2.
其次在开一组循环,根据x,y的值(即K-1的坐标)情况判断怎么填写数字,在填写数字完毕后将x,y的值赋为当前坐标。
最后当循环数到达n*n后结束并输出
#include<bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N][N];
int main()
{
int n;cin>>n;
int x=1,y=(n+1)/2;
a[x][y]=1;
for(int i=2; i<=n*n; i++)
{
if(x==1&&y!=n)
{
x=n;
y++;
}
else if(x!=1&&y==n)
{
x--;
y=1;
}
else if(x==1&&y==n) x++;
else if(!a[x-1][y+1])
{
x--;
y++;
}
else x++;
a[x][y]=i;
}
for(int i=1; i<=n; i++)
{
cout << a[i][1];
for (int j=2; j<=n; j++) cout << ' ' << a[i][j];
cout << endl;
}
return 0;
}
边栏推荐
- Talking about 3 common shadow rendering techniques in games (1): plane shadow
- ArgumentException: GetComponent requires that the requested component ‘GameObject‘ derives from Mono
- Unity plug-in DOTween User Guide 2 (Brief explanation of Bezier curves)
- DRM Memory Management
- 强化学习_06_DataWhale深度Q网络
- 内核映像文件格式
- 如何在VMlogin中设置YiLu代理?
- How is C# hot update better than Lua?
- 动态规划、背包问题 6/26 116-120
- 链表、栈、队列
猜你喜欢
随机推荐
Unity热更新哪些事
lua循环
动态规划——从0-1背包问题到leetcode正则匹配
Qt信号槽与事件循环的关系
OpenGL学习笔记(LearnOpenGL)-第六部分 变换
观察者模式-数据池
Unity screen coordinates to world coordinates, mouse click to get 3D position
Teach you to change the kernel source code--sysfs virtual file system 2
Screen post-processing: Sobel operator to achieve edge detection
虚幻5简单第三人称游戏制作文档
UnityShader入门精要-渲染纹理 镜子 玻璃 效果
Unity object pool implementation
进制的前缀表示和后缀表示
二次元卡通渲染-着色
Unity2D动画生成操作(简单)
高质量WordPress下载站模板5play主题
BUUCTF笔记(web)
酸回收工艺讲解
Talking about 3 common shadow rendering techniques in games (2): shadow cone
Analysis of minix_super_block.s_ninodes of mkfs.minix.c









