当前位置:网站首页>2022.02.14
2022.02.14
2022-07-03 22:55:00 【Walking code Jun】
Today I wrote an interesting topic , People are numb
Input : The first line is an integer , Indicates the number of test cases . Every subsequent line , There is an integer n(1 < = n < = 30 ).
Output : For each n×n Square array , Please refer to the output sample , Output coiled dragon array . To align , Each array element occupies 4 Character width , also , Insufficient 4 Press right to align the output of characters . There is a blank line after the output of each test case . Note that there are no spaces at the end of each line .
### Input examples
3 1 2 5
### Output example
1 1 2 4 3 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
This question is OK , Of course, the process is troublesome
#include<stdio.h>
#include<string.h>
int main()
{
int n,m,i,j,k,a[100][100]={0};
int num;
scanf("%d",&n);
while(n--)
{
num=1;
scanf("%d",&m);
for(j=0;j<=m/2;j++)
{
for(k=j;k<m-j;k++)
a[j][k]=num++;
for(k=j+1;k<m-j-1;k++)
a[k][m-j-1]=num++;
for(k=m-j;k>j;k--)
a[m-1-j][k-1]=num++;
for(k=m-j-2;k>j;k--)
a[k][j]=num++;
}
if(m%2!=0)
a[m/2][m/2]=m*m;
for(i=0;i<m;i++)
{
for(k=0;k<m;k++)
printf("%4d",a[i][k]);
printf("\n");
}
printf("\n");
}
return 0;
}
premium :
Problem description
To take a number from a loop is to take a number along the edge of a matrix , If the current direction of countless or have taken , Turn left 90 degree . It starts at the top left corner of the matrix , Direction down .
Input format
The first line of input is no more than 200 The positive integer m, n, The rows and columns that represent the matrix . Next m Every line n It's an integer , To represent this matrix .
Output format
There is only one line of output , common mn Number , The result of taking data for the input matrix . Numbers are separated by a space , Don't have extra space at the end of line .
The sample input
3 3
1 2 3
4 5 6
7 8 9
Sample output
1 4 7 8 9 6 3 2 5
The sample input
3 2
1 2
3 4
5 6
Sample output
1 3 5 6 4 2
At first glance, there is no difference between the two questions , But the feeling is different ( The child has fainted in the toilet )
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m,a[300][300],i,j;
cin>>n>>m;
int temp;
if(n>=m)
temp=m;
else
temp=n;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
cin>>a[i][j];
}
if(n==1||m==1)
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(i==0&&j==0)
cout<<a[i][j];
else
cout<<" "<<a[i][j];
}
}
else
{
for(i=0;i<(temp+1)/2;i++)
{
if(i==(temp+1)/2-1&&temp==n&&temp%2!=0)
{
for(j=i;j<m-i;j++)
cout<<" "<<a[n/2][j];
break;
}
if(i==(temp+1)/2-1&&temp==m&&temp%2!=0)
{
for(j=i;j<n-i;j++)
cout<<" "<<a[j][m/2];
break;
}
for(j=i;j<n-i-1;j++)
{
if(i==0&&j==0)
cout<<a[j][i];
else
cout<<" "<<a[j][i];
}
for(j=i;j<m-i-1;j++)
cout<<" "<<a[n-1-i][j];
for(j=n-1-i;j>i;j--)
cout<<" "<<a[j][m-1-i];
for(j=m-i-1;j>i;j--)
cout<<" "<<a[i][j];
}
}
return 0;
}
It's easy to get wrong :1. To determine whether the horizontal and vertical coordinates of the array are 1, namely :int a【1】【x】 or int a【x】【1】;
2. Print back to determine the parity of the horizontal and vertical coordinates of the array ;
3. The number of outermost cycles should be judged by the size of the horizontal and vertical coordinates ;
Two and a half hours just to get one question right , I can't write anymore .
边栏推荐
- Teach you to easily learn the type of data stored in the database (a must see for getting started with the database)
- Preliminary analysis of smart microwave radar module
- Pyqt5 sensitive word detection tool production, operator's Gospel
- 4 environment construction -standalone ha
- Programming language (1)
- Pooling idea: string constant pool, thread pool, database connection pool
- LeetCode 1646. Get the maximum value in the generated array
- NPM script
- How to switch between dual graphics cards of notebook computer
- Hcip day 16 notes
猜你喜欢
Pan Yueming helps Germany's Rochester Zodiac custom wristwatch
MLX90614 driver, function introduction and PEC verification
Unique in China! Alibaba cloud container service enters the Forrester leader quadrant
C deep anatomy - the concept of keywords and variables # dry inventory #
[dynamic programming] Ji Suan Ke: Suan tou Jun breaks through the barrier (variant of the longest increasing subsequence)
Hcip 13th day notes
2 spark environment setup local
Ppt image processing
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
How can enterprises and developers take advantage of the explosion of cloud native landing?
随机推荐
IPhone development swift foundation 09 assets
Cesium terrain clipping draw polygon clipping
Classification and extension of OC
LeetCode 1647. Minimum deletion times of unique character frequency
Unique in China! Alibaba cloud container service enters the Forrester leader quadrant
1068. Consolidation of ring stones (ring, interval DP)
How about agricultural futures?
3 environment construction -standalone
Blue Bridge Cup -- Mason prime
[Android reverse] use the DB browser to view and modify the SQLite database (copy the database file from the Android application data directory | use the DB browser tool to view the data block file)
IO flow review
Blue Bridge Cup Guoxin Changtian MCU -- program download (III)
The 2022 global software R & D technology conference was released, and world-class masters such as Turing prize winners attended
Subset enumeration method
Hcip day 14 notes
Firefox set up proxy server
Buuctf, web:[geek challenge 2019] buyflag
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Opengauss database log management guide
油猴插件