当前位置:网站首页>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");
}
}
边栏推荐
- 看过很多教程,却依然写不好一个程序,怎么破?
- Gui Gui programming (XIII) - event handling
- 自定义限流注解
- B-杰哥的树(状压树形dp)
- 【GET-4】
- HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
- Ideas and methods of system and application monitoring
- 小孩子学什么编程?
- I've seen many tutorials, but I still can't write a program well. How can I break it?
- 【DSP】【第一篇】开始DSP学习
猜你喜欢

What key progress has been made in deep learning in 2021?
![[network planning] Chapter 3 data link layer (3) channel division medium access control](/img/df/dd84508dfa2449c31d72c808c50dc0.png)
[network planning] Chapter 3 data link layer (3) channel division medium access control

HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother

Logic is a good thing

【微信小程序】运行机制和更新机制

use. Net analysis Net talent challenge participation

(work record) March 11, 2020 to March 15, 2021

看过很多教程,却依然写不好一个程序,怎么破?

【DSP】【第二篇】了解C6678和创建工程

Web security - payload
随机推荐
动态切换数据源
(work record) March 11, 2020 to March 15, 2021
7、数据权限注解
Rhcsa Road
8086指令码汇总表(表格)
Detailed explanation of knowledge map construction process steps
BeagleBoneBlack 上手记
Database - how to get familiar with hundreds of tables of the project -navicat these unique skills, have you got it? (exclusive experience)
知识图谱构建流程步骤详解
Solution to the 38th weekly match of acwing
Dynamically switch data sources
Tencent byte Alibaba Xiaomi jd.com offer got a soft hand, and the teacher said it was great
JS get browser system language
Implementation of packaging video into MP4 format and storing it in TF Card
Is it difficult for small and micro enterprises to make accounts? Smart accounting gadget quick to use
Design your security architecture OKR
Node. Js: express + MySQL realizes registration, login and identity authentication
Case ① | host security construction: best practice of 3 levels and 11 capabilities
Error analysis ~csdn rebound shell error
How to upgrade high value-added links in the textile and clothing industry? APS to help