当前位置:网站首页>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 9
Compare 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");
}
}
边栏推荐
- 报错分析~csdn反弹shell报错
- 永磁同步电机转子位置估算专题 —— 基波模型与转子位置角
- Le lancement du jupyter ne répond pas après l'installation d'Anaconda
- [DSP] [Part 1] start DSP learning
- BUUCTF---Reverse---easyre
- 【每周一坑】信息加密 +【解答】正整数分解质因数
- Design your security architecture OKR
- JVM_ Common [interview questions]
- Application layer of tcp/ip protocol cluster
- Continuous test (CT) practical experience sharing
猜你喜欢
How to upgrade high value-added links in the textile and clothing industry? APS to help
Build your own application based on Google's open source tensorflow object detection API video object recognition system (IV)
RT thread I2C tutorial
Logic is a good thing
Anaconda安裝後Jupyter launch 沒反應&網頁打開運行沒執行
Core principles of video games
【GET-4】
逻辑是个好东西
[weekly pit] positive integer factorization prime factor + [solution] calculate the sum of prime numbers within 100
(工作记录)2020年3月11日至2021年3月15日
随机推荐
【DSP】【第一篇】开始DSP学习
Special topic of rotor position estimation of permanent magnet synchronous motor -- Summary of position estimation of fundamental wave model
(工作记录)2020年3月11日至2021年3月15日
Case ① | host security construction: best practice of 3 levels and 11 capabilities
Basic knowledge of lists
Ideas and methods of system and application monitoring
5. Wireless in vivo nano network: top ten "feasible?" problem
OLED屏幕的使用
Web security - payload
【DSP】【第二篇】了解C6678和创建工程
7、数据权限注解
Appx code signing Guide
Solution to the 38th weekly match of acwing
02 基础入门-数据包拓展
Tencent T4 architect, Android interview Foundation
Extraction rules and test objectives of performance test points
【微信小程序】運行機制和更新機制
[wechat applet] operation mechanism and update mechanism
Recyclerview not call any Adapter method :onCreateViewHolder,onBindViewHolder,
I've seen many tutorials, but I still can't write a program well. How can I break it?