ZSTUOJ平台刷题⑤:Problem G.--深入浅出学算法023-旋转数阵
2024-10-22 04:53:48
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;
}
最新文章
- kafka与传统的消息中间件对比
- ios基础篇(二十五)—— Animation动画(UIView、CoreAnimation)
- 用canvas画“哆啦A梦”时钟
- 尝试在mac上用dotnet cli运行asp.net core示例程序
- MVC 自定义异常错误页面处理
- self-sizing cell的一个问题
- CCF-Crontab-201712-3
- xlistview长按
- Json Schema的使用
- 使用ntlmrelayx在任何地方进行中继凭据
- libusb开发
- springboot项目的重定向和转发
- RIDE指定log和report的输出目录
- sublime相关资源
- [ZJOI2011]细胞——斐波那契数列+矩阵加速+dp
- java代码优化29个点
- Struts的线程安全
- Master 接受其它组件的注册
- SetUnhandledExceptionFilter
- 使用Process组件访问本地进程