当前位置:网站首页>Spiral square PTA
Spiral square PTA
2022-07-06 20:38:00 【Sophomore to major】
So-called “ Spiral square ”, It refers to any given N, take 1 To N×N The number from the top left corner is 1 Start with a grid , Fill in in clockwise spiral direction N×N In the square of . This problem requires the construction of such a spiral square matrix .
Input format :
Input gives a positive integer on a line N(<10).
Output format :
Output N×N Spiral matrix of . Each row N A digital , Each number accounts for 3 position .
sample input :
5
sample output :
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9Compare the two ideas :
What I think is to the right , Down , towards the left , Up to the end , Set flag subscript , Proceed to the next step .
#include <stdio.h>
int main(){
int n,arr[100][100]={0};
scanf("%d",&n);
int i=0,j=-1,k=1,zuo=0,shang=1;
while(k<=n*n&&n!=1){
while(1){
if(arr[i][j]==n*n){break;}
j++;
arr[i][j]=k++;
if(j==n-i-1){
break;
}
}
while(1){
if(arr[i][j]==n*n){break;}
i++;
arr[i][j]=k++;
if(i==j){
break;
}
}
while(1){
if(arr[i][j]==n*n){break;}
j--;
arr[i][j]=k++;
if(j==zuo){
zuo++;
break;
}
}
while(1){
if(arr[i][j]==n*n){break;}
i--;
arr[i][j]=k++;
if(i==shang){
shang++;
break;
}
}
}
if(n==1){arr[0][0]=1;}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
printf("%3d",arr[i][j]);
}
printf("\n");
}
return 0;
}Learn the thought of the great God , Upper left and lower right n-1 individual , Then operate ;
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int arr[n][n],i,j,k=1,c=n-1,x=0,y=0;
for(i=0;i<n/2;i++)
{
for(j=0;j<c;j++) arr[x][y++] = k++;
for(j=0;j<c;j++) arr[x++][y] = k++;
for(j=0;j<c;j++) arr[x][y--] = k++;
for(j=0;j<c;j++) arr[x--][y] = k++;
x++;y++;c-=2;
}
if(n%2) arr[n/2][n/2] = k;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%3d",arr[i][j]);
}
printf("\n");
}
}
边栏推荐
- 强化学习-学习笔记5 | AlphaGo
- Can novices speculate in stocks for 200 yuan? Is the securities account given by qiniu safe?
- [diy] how to make a personalized radio
- 报错分析~csdn反弹shell报错
- Detailed explanation of knowledge map construction process steps
- 【每周一坑】信息加密 +【解答】正整数分解质因数
- B-杰哥的树(状压树形dp)
- What key progress has been made in deep learning in 2021?
- 深度学习分类网络 -- ZFNet
- 永磁同步电机转子位置估算专题 —— 基波模型与转子位置角
猜你喜欢
![[cloud native and 5g] micro services support 5g core network](/img/c9/4ccacd1e70285c2ceb50c324e5018c.png)
[cloud native and 5g] micro services support 5g core network

I've seen many tutorials, but I still can't write a program well. How can I break it?

Entity alignment two of knowledge map

Design your security architecture OKR

Basic knowledge of lists
![[DIY]如何制作一款個性的收音機](/img/fc/a371322258131d1dc617ce18490baf.jpg)
[DIY]如何制作一款個性的收音機

01 基础入门-概念名词

“罚点球”小游戏
![[network planning] Chapter 3 data link layer (4) LAN, Ethernet, WLAN, VLAN](/img/b8/3d48e185bb6eafcdd49889f0a90657.png)
[network planning] Chapter 3 data link layer (4) LAN, Ethernet, WLAN, VLAN
![[Yann Lecun likes the red stone neural network made by minecraft]](/img/95/c3af40c7ecbd371dd674aea19b272a.png)
[Yann Lecun likes the red stone neural network made by minecraft]
随机推荐
Build your own application based on Google's open source tensorflow object detection API video object recognition system (IV)
Implementation of packaging video into MP4 format and storing it in TF Card
2110 summary of knowledge points and common problems in redis class
Deep learning classification network -- zfnet
自定义限流注解
BeagleBoneBlack 上手记
Common doubts about the introduction of APS by enterprises
Summary of different configurations of PHP Xdebug 3 and xdebug2
Maximum likelihood estimation and cross entropy loss
Notes on beagleboneblack
B-杰哥的树(状压树形dp)
APS taps home appliance industry into new growth points
Initial experience of addresssanitizer Technology
What programming do children learn?
Ideas and methods of system and application monitoring
In unity space, an object moves around a fixed point on the sphere at a fixed speed
棋盘左上角到右下角方案数(2)
【每周一坑】计算100以内质数之和 +【解答】输出三角形
Rhcsa Road
Quel genre de programmation les enfants apprennent - ils?