当前位置:网站首页>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 .
边栏推荐
- This time, thoroughly understand bidirectional data binding 01
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- [dynamic planning] counting garlic customers: the log of garlic King (the longest increasing public subsequence)
- How about opening an account at Hengtai securities? Is it safe?
- Codeforces Round #768 (Div. 1)(A-C)
- IPhone development swift foundation 08 encryption and security
- LeetCode 540. A single element in an ordered array
- What indicators should be paid attention to in current limit monitoring?
- [automation operation and maintenance novice village] flask-2 certification
- Comparable interface and comparator interface
猜你喜欢

string

Hcip day 16 notes

Scratch uses runner Py run or debug crawler

Buuctf, misc: n solutions

Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?

User login function: simple but difficult

Blue Bridge Cup -- guess age

The overseas listing of Shangmei group received feedback, and brands such as Han Shu and Yiye have been notified for many times and received attention

Overview of Yunxi database executor

Data consistency between redis and database
随机推荐
How to solve win10 black screen with only mouse arrow
To rotate 90 degrees clockwise and modify the video format
In VS_ In 2019, scanf and other functions are used to prompt the error of unsafe functions
Data consistency between redis and database
Shell script three swordsman awk
Sort merge sort
3 environment construction -standalone
QT creator source code learning note 05, how does the menu bar realize plug-in?
Ansible common usage scenarios
Teach you to easily learn the type of data stored in the database (a must see for getting started with the database)
. Net ADO splicing SQL statement with parameters
How to solve the problem of computer networking but showing no Internet connection
Yyds dry goods inventory Prometheus alarm Art
Buuctf, web:[geek challenge 2019] buyflag
Label coco format data and format data in the upper left corner and lower right corner are mutually converted
Classification and extension of OC
Learning notes of raspberry pie 4B - IO communication (SPI)
LeetCode 1646. Get the maximum value in the generated array
C deep anatomy - the concept of keywords and variables # dry inventory #
Mongoose the table associated with the primary key, and automatically bring out the data of another table