当前位置:网站首页>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");
}
}
边栏推荐
- Basic knowledge of lists
- Deep learning classification network -- zfnet
- Value of APS application in food industry
- [network planning] Chapter 3 data link layer (3) channel division medium access control
- Kubernetes learning summary (20) -- what is the relationship between kubernetes and microservices and containers?
- Recyclerview not call any Adapter method :onCreateViewHolder,onBindViewHolder,
- Quel genre de programmation les enfants apprennent - ils?
- C language games - minesweeping
- 7、数据权限注解
- [DIY]如何制作一款個性的收音機
猜你喜欢
PHP online examination system version 4.0 source code computer + mobile terminal
02 basic introduction - data package expansion
Common doubts about the introduction of APS by enterprises
强化学习-学习笔记5 | AlphaGo
Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]
[DIY]如何制作一款個性的收音機
RT thread I2C tutorial
BeagleBoneBlack 上手记
逻辑是个好东西
[weekly pit] output triangle
随机推荐
I've seen many tutorials, but I still can't write a program well. How can I break it?
[weekly pit] positive integer factorization prime factor + [solution] calculate the sum of prime numbers within 100
【每周一坑】正整数分解质因数 +【解答】计算100以内质数之和
【每周一坑】信息加密 +【解答】正整数分解质因数
01 基础入门-概念名词
BeagleBoneBlack 上手记
5. Wireless in vivo nano network: top ten "feasible?" problem
【微信小程序】运行机制和更新机制
Leetcode question 448 Find all missing numbers in the array
2022 refrigeration and air conditioning equipment installation and repair examination contents and new version of refrigeration and air conditioning equipment installation and repair examination quest
【GET-4】
Case ① | host security construction: best practice of 3 levels and 11 capabilities
Event center parameter transfer, peer component value transfer method, brother component value transfer
数字三角形模型 AcWing 1018. 最低通行费
Anaconda安装后Jupyter launch 没反应&网页打开运行没执行
Why do novices often fail to answer questions in the programming community, and even get ridiculed?
SQL injection 2
Logic is a good thing
[weekly pit] calculate the sum of primes within 100 + [answer] output triangle
use. Net analysis Net talent challenge participation