# "Niuke.com C" beginners training BC134, BC136​

🐶Blogger homepage: @ᰔᩚ. A pregnant moon ꦿ

❤️‍🔥Column series: Linear AlgebraC Beginners Introductory Training

🔥Motto: "Don't wait until you have nothing before making up your mind to do it"

🚀🚀🚀If everyone thinks it’s good, I implore everyone to pay attention, a little love, and give pointers🚀🚀🚀​​​​​​​​

topic: BC134 snake matrix

Difficulty: Moderate

describe:

Given an integer n, output an n∗n snake matrix.

Enter a description:

Enter a line containing an integer n

Output description:

Output n lines, each line contains n positive integers separated by spaces.

1<=n<=1000

Example 1

enter:

4

output:

1 2 6 7

3 5 8 13

4 9 12 14

10 11 15 16

Problem-solving idea: Therefore, I use the pos variable to represent the direction, where 1 represents upper right and right, and -1 represents lower left and lower.

There are 6 types of exercise:

Hit the upper boundary (that is, i is equal to 1 and j is less than n): the column increases by 1, and the row remains the same -------- move one grid to the right

Touch the left border (ie j is equal to 1 and i is less than n): the row increases by 1, and the column remains unchanged -------- move down one grid

Touch the right border (ie j is equal to n): the row increases by 1, and the column remains unchanged --------- move down one grid

Hit the lower boundary (i is equal to n): the column increases by 1, the row remains the same --------- move one grid to the right

The above four are moving above the boundary. Only right and down

Except for the above four boundary conditions, it is to move to the lower left in the middle of the boundary

Except for the above four boundary conditions, it is to move to the upper right in the middle of the boundary

The last two are moving within the bounds only top right and bottom left

```#include<stdio.h>
int main()
{
int n=0;
printf("Please enter the order of the matrix\n");
scanf("%d",&n);
int arr[n+2][n+2];//Although a matrix of order n+2 is defined, we only need a matrix of order n
for(int i=0;i<n+2;i++)
{
for(int j=0;j<n+2;j++)
{
arr[i][j]=0;
}
}
arr[1][1]=1;
int k=0;
int i=1,j=1,pos=1;
for(k=2;k<=n*n;k++)
{
if(i==1&&j<n&&pos==1)//upper boundary, the row remains unchanged, and the column increases by one
{
arr[i][++j]=k;
pos=-1;
}
else if(j==1&&i<n&&pos==-1)//Left border, column unchanged, row increased by one
{
arr[++i][j]=k;
pos=1;
}
else if(j==n&&pos==1)//Right border, row plus one, column unchanged
{
arr[++i][j]=k;
pos=-1;
}
else if(i==n&&pos==-1)//Lower boundary, row unchanged, column plus one
{
arr[i][++j]=k;
pos=1;
}
else if(pos==1)//upper right
{
arr[--i][++j]=k;
}
else if(pos==-1)//bottom left
{
arr[++i][--j]=k;
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%-4d",arr[i][j]);
}
printf("\n");
}
}
```

BC136 KiKi judges the upper triangular matrix

Difficulty: Moderate

describe:

KiKi wants to know whether an nth order square moment is an upper triangular matrix, please help him program the judgment. An upper triangular matrix is ​​a matrix in which all elements below the main diagonal are 0, and the main diagonal is a line from the upper left corner of the matrix to the lower right corner.

Enter a description:

The first line contains an integer n, indicating that a square matrix contains n rows and n columns, separated by spaces. (2≤n≤10)

From line 2 to n+1, enter n integers (range -231~231-1) in each line, separated by spaces, and enter n*n numbers in total.

Output description:

One line, if the input square matrix is ​​an upper triangular matrix, output "YES" and wrap, otherwise output "NO" and wrap.

Example 1

enter:

3

1 2 3

0 4 5

0 0 6

output:

YES

Example 2

enter:

4

1 2 3 4

5 6 7 8

9 0 11 12

13 0 0 16

output:

NO

Problem-solving idea: We can judge whether the elements below its main diagonal are 0, the first row judges 0 elements, the second row judges two elements, the third row judges three elements...the nth row judges n-1 elements.

```#include<stdio.h>
int main()
{
int n=0;
printf("Enter the side length of the input square matrix\n");
scanf("%d",&n);
int arr[n][n];
int i=0,j=0;
int count=0;
for( i=0;i<n;i++)
{
for( j=0;j<n;j++)
{
scanf("%d",&arr[i][j]);
}
}
for( i=1;i<n;i++)
{
for( j=0;j<i;j++)
{
if(arr[i][j]!=0)
{
printf("NO\n");
count=1;
goto agin;
}
}
}
agin:
if(count==0)
{
printf("YES\n");
}
return 0;
}```

The goto statement is used here. If it is judged that one of its elements is not 0, it will directly jump out of the double loop, which reduces the number of loops.

🌸🌸🌸 If you still don't understand or have suggestions, you can post them in the comment area. We will discuss, learn and make progress together. thank you all! 🌸🌸🌸

Posted by php_coder_dvo on Wed, 01 Mar 2023 04:02:58 +0530