当前位置:网站首页>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 .
边栏推荐
- Recursion and recursion
- Programming language (1)
- To rotate 90 degrees clockwise and modify the video format
- The difference between SRAM and DRAM
- string
- Why should enterprises do more application activities?
- Is the controller a single instance or multiple instances? How to ensure the safety of concurrency
- Yyds dry goods inventory Spring Festival "make" your own fireworks
- Yyds dry goods inventory Prometheus alarm Art
- Summary of basic knowledge of exception handling
猜你喜欢
![[automation operation and maintenance novice village] flask-2 certification](/img/9a/a9b45e1f41b9b75695dcb06c212a69.jpg)
[automation operation and maintenance novice village] flask-2 certification

Qtoolbutton - menu and popup mode

Ppt image processing

Pan Yueming helps Germany's Rochester Zodiac custom wristwatch

Leetcode: a single element in an ordered array

Hcip day 12 notes

IO flow review
![Buuctf, web:[geek challenge 2019] buyflag](/img/02/d3add04f8145621bff35d46b82ba53.jpg)
Buuctf, web:[geek challenge 2019] buyflag

Pat grade A - 1164 good in C (20 points)

Pointer concept & character pointer & pointer array yyds dry inventory
随机推荐
What are the common computer problems and solutions
JarPath
How can enterprises and developers take advantage of the explosion of cloud native landing?
Mysql database - Advanced SQL statement (I)
Label coco format data and format data in the upper left corner and lower right corner are mutually converted
Quick one click batch adding video text watermark and modifying video size simple tutorial
Oil monkey plug-in
Buuctf, misc: sniffed traffic
Mongoose the table associated with the primary key, and automatically bring out the data of another table
Blue Bridge Cup Guoxin Changtian MCU -- program download (III)
A preliminary study on the middleware of script Downloader
Is it safe and reliable to open an account and register for stock speculation? Is there any risk?
[flax high frequency question] leetcode 426 Convert binary search tree to sorted double linked list
How to restore the factory settings of HP computer
Esp-idf turns off serial port log output.
Sow of PMP
Niuke winter vacation training camp 4 g (enumeration optimization, Euler power reduction)
Mindmanager2022 serial number key decompression installer tutorial
URLEncoder. Encode and urldecoder Decode processing URL
Runtime. getRuntime(). totalMemory/maxMemory()