当前位置:网站首页>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 .
边栏推荐
- Yyds dry goods inventory Spring Festival "make" your own fireworks
- Hcip day 14 notes
- What indicators should be paid attention to in current limit monitoring?
- Blue Bridge Cup -- guess age
- Ten minutes will take you in-depth understanding of multithreading. Multithreading on lock optimization (I)
- Shiftvit uses the precision of swing transformer to outperform the speed of RESNET, and discusses that the success of Vit does not lie in attention!
- SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]
- Get current JVM data
- Leetcode: a single element in an ordered array
- How to restore the factory settings of HP computer
猜你喜欢

3 environment construction -standalone

Overview of Yunxi database executor

Data consistency between redis and database

Flutter internationalized Intl

How can enterprises and developers take advantage of the explosion of cloud native landing?

Quick one click batch adding video text watermark and modifying video size simple tutorial

Exness: the Central Bank of England will raise interest rates again in March, and inflation is coming

Firefox set up proxy server

Data consistency between redis and database

Hcip day 15 notes
随机推荐
Mindmanager2022 serial number key decompression installer tutorial
STM32 multi serial port implementation of printf -- Based on cubemx
. Net ADO splicing SQL statement with parameters
Pyqt5 sensitive word detection tool production, operator's Gospel
The reason why the computer runs slowly and how to solve it
540. Single element in ordered array
This time, thoroughly understand bidirectional data binding 01
Xiangong intelligent obtained hundreds of millions of yuan of b-round financing to accelerate the process of building non-standard solutions with standardized products
How to solve the problem of computer networking but showing no Internet connection
Programming language (1)
[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)
Shell script three swordsman awk
Data consistency between redis and database
C3p0 connection MySQL 8.0.11 configuration problem
Hcip day 12 notes
Esp-idf turns off serial port log output.
[automation operation and maintenance novice village] flask-2 certification
1068. Consolidation of ring stones (ring, interval DP)
In VS_ In 2019, scanf and other functions are used to prompt the error of unsafe functions
Hcip day 14 notes