当前位置:网站首页>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 .
边栏推荐
- Sort merge sort
- Flutter internationalized Intl
- Mindmanager2022 serial number key decompression installer tutorial
- Pat grade A - 1164 good in C (20 points)
- [Android reverse] use DB browser to view and modify SQLite database (download DB browser installation package | install DB browser tool)
- Pyqt5 sensitive word detection tool production, operator's Gospel
- This time, thoroughly understand bidirectional data binding 01
- Buuctf, web:[geek challenge 2019] buyflag
- How to solve the problem of requiring a password when accessing your network neighborhood on your computer
- Classification and extension of OC
猜你喜欢
Qtoolbutton - menu and popup mode
Blue Bridge Cup -- guess age
webAssembly
Unique in China! Alibaba cloud container service enters the Forrester leader quadrant
How to connect a laptop to a projector
Pyqt5 sensitive word detection tool production, operator's Gospel
1 Introduction to spark Foundation
Teach you to easily learn the type of data stored in the database (a must see for getting started with the database)
Go Technology Daily (2022-02-13) - Summary of experience in database storage selection
Firefox set up proxy server
随机推荐
Exclusive download! Alibaba cloud native brings 10 + technical experts to bring "new possibilities of cloud native and cloud future"
Why should enterprises do more application activities?
1068. Consolidation of ring stones (ring, interval DP)
[flax high frequency question] leetcode 426 Convert binary search tree to sorted double linked list
[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)
2.14 summary
Recursion and recursion
Harbor integrated LDAP authentication
LeetCode 540. A single element in an ordered array
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
MLX90614 driver, function introduction and PEC verification
SDMU OJ#P19. Stock trading
. Net ADO splicing SQL statement with parameters
Yyds dry goods inventory Spring Festival "make" your own fireworks
IDENTITY
On my first day at work, this API timeout optimization put me down!
To rotate 90 degrees clockwise and modify the video format
How can enterprises and developers take advantage of the explosion of cloud native landing?
Firefox set up proxy server
NPM script