当前位置:网站首页>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");
}
}
边栏推荐
- [cloud lesson] EI lesson 47 Mrs offline data analysis - processing OBS data through Flink
- Application layer of tcp/ip protocol cluster
- Wechat applet common collection
- JS get browser system language
- 22-07-05 upload of qiniu cloud storage pictures and user avatars
- Introduction of Xia Zhigang
- 为什么新手在编程社区提问经常得不到回答,甚至还会被嘲讽?
- RT thread I2C tutorial
- 7. Data permission annotation
- Is it safe to open an account in flush? Which securities company is good at opening an account? Low handling charges
猜你喜欢
[cloud native and 5g] micro services support 5g core network
【Yann LeCun点赞B站UP主使用Minecraft制作的红石神经网络】
Tencent byte Alibaba Xiaomi jd.com offer got a soft hand, and the teacher said it was great
Maximum likelihood estimation and cross entropy loss
2022 construction electrician (special type of construction work) free test questions and construction electrician (special type of construction work) certificate examination
Tencent byte and other big companies interview real questions summary, Netease architects in-depth explanation of Android Development
[DSP] [Part 2] understand c6678 and create project
【每周一坑】正整数分解质因数 +【解答】计算100以内质数之和
永磁同步电机转子位置估算专题 —— 基波模型与转子位置角
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
随机推荐
8086指令码汇总表(表格)
C language games - three chess
Enumeration gets values based on parameters
01 基础入门-概念名词
use. Net analysis Net talent challenge participation
Trends of "software" in robotics Engineering
Discussion on beegfs high availability mode
【每周一坑】信息加密 +【解答】正整数分解质因数
OAI 5G NR+USRP B210安装搭建
2022 Guangdong Provincial Safety Officer C certificate third batch (full-time safety production management personnel) simulation examination and Guangdong Provincial Safety Officer C certificate third
Maximum likelihood estimation and cross entropy loss
[weekly pit] positive integer factorization prime factor + [solution] calculate the sum of prime numbers within 100
Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
使用.Net分析.Net达人挑战赛参与情况
Detailed introduction of distributed pressure measurement system VIII: basic introduction of akka actor model
Number of schemes from the upper left corner to the lower right corner of the chessboard (2)
Can novices speculate in stocks for 200 yuan? Is the securities account given by qiniu safe?
Unity makes AB package
[cloud lesson] EI lesson 47 Mrs offline data analysis - processing OBS data through Flink
Case ① | host security construction: best practice of 3 levels and 11 capabilities