Problem G: 深入浅出学算法023-旋转数阵

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 4794  Solved: 955

Description

把1到n2的正整数从左上角开始由外层至中心按照顺时针方向螺旋排列

Input

输入整数n (1 <= n <= 10)

Output

按示例输出矩阵

Sample Input

3
4

Sample Output

1 2 3
8 9 4
7 6 5
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
代码如下:
#include<bits/stdc++.h>
using namespace std; int main(){
int a[11][11];
int n;
while(~scanf("%d",&n)){
memset(a,0,sizeof(a));
int x,y,tot=0;
a[x=0][y=0]=tot=1;
while(tot<n*n){
while(y+1<n&&!a[x][y+1]) a[x][++y]=++tot;
while(x+1<n&&!a[x+1][y]) a[++x][y]=++tot;
while(y-1>=0&&!a[x][y-1]) a[x][--y]=++tot;
while(x-1>=0&&!a[x-1][y]) a[--x][y]=++tot;
}
if(n<=3){
for(x=0;x<n;x++){
for(y=0;y<n;y++){
if(y==0) printf("%d",a[x][y]);
else printf("%2d",a[x][y]);
}
printf("\n");
}
}
else if(n==10){
for(x=0;x<n;x++){
for(y=0;y<n;y++){
if(y==0) printf("%3d",a[x][y]);
else printf("%4d",a[x][y]);
}
printf("\n");
}
}
else{
for(x=0;x<n;x++){
for(y=0;y<n;y++){
if(y==0) printf("%2d",a[x][y]);
else printf("%3d",a[x][y]);
}
printf("\n");
}
}
} return 0;
}

最新文章

  1. kafka与传统的消息中间件对比
  2. ios基础篇(二十五)—— Animation动画(UIView、CoreAnimation)
  3. 用canvas画“哆啦A梦”时钟
  4. 尝试在mac上用dotnet cli运行asp.net core示例程序
  5. MVC 自定义异常错误页面处理
  6. self-sizing cell的一个问题
  7. CCF-Crontab-201712-3
  8. xlistview长按
  9. Json Schema的使用
  10. 使用ntlmrelayx在任何地方进行中继凭据
  11. libusb开发
  12. springboot项目的重定向和转发
  13. RIDE指定log和report的输出目录
  14. sublime相关资源
  15. [ZJOI2011]细胞——斐波那契数列+矩阵加速+dp
  16. java代码优化29个点
  17. Struts的线程安全
  18. Master 接受其它组件的注册
  19. SetUnhandledExceptionFilter
  20. 使用Process组件访问本地进程

热门文章

  1. Spring入门之使用 spring 的 IOC 解决程序耦合(Spring 基于 XML 的 IOC 细节[掌握])(03-02)
  2. 小白之Python-基础中的基础01
  3. Linux df -h 显示磁盘空间满,但实际未占用满——问题分析
  4. 求助:我需要用Python中parsel模块提取文章的文本内容,有什么办法
  5. 剑指 Offer II 动态规划
  6. [转并修改]C#编程中跨线程访问控件
  7. DAC双通道模板
  8. Centos 性能监控技巧
  9. 正则表达式re.compile()的使用
  10. linux安装EMQ