【NOIP2015提高组】Day1 t1神奇的幻方

 

一大淼题,直接瞎搞即可,不过一定要仔细看题目给定的条件。

复制代码
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #define M 50
 5 using namespace std;
 6 int a[M][M]={0};
 7 int main(){
 8     freopen("magic.in","r",stdin);
 9     freopen("magic.out","w",stdout);
10     int n; cin>>n;
11     int lastx=1,lasty=1+(n>>1); a[lastx][lasty]=1;
12     for(int i=2;i<=n*n;i++){
13         if(lastx==1&&lasty!=n){a[lastx=n][++lasty]=i; continue;}
14         if(lastx!=1&&lasty==n){a[--lastx][lasty=1]=i; continue;}
15         if(lastx==1&&lasty==n){a[++lastx][lasty]=i; continue;} 
16         if(lastx!=1&&lasty!=n){
17             if(a[lastx-1][lasty+1]==0) 
18             a[--lastx][++lasty]=i; 
19             else a[++lastx][lasty]=i;
20             continue;
21         }
22     }
23     for(int i=1;i<=n;i++){
24         for(int j=1;j<=n;j++) printf("%d ",a[i][j]);
25         printf("\n");
26     }
27 }
复制代码

 

posted @   AlphaInf  阅读(301)  评论(2编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示