[C/PTA] Advanced Array Exercises (1)

This article combines PTA special exercises to guide readers to master arrays. The main topic is supplemented by comments, so that readers can understand the ideas in the code. Other details will not be described too much.

7-1 Matrix operations

Given an n×n square matrix, this question requires calculating the sum of all elements of the matrix except the sub-diagonal, the last column and the last row. The subdiagonal line is the line from the upper right corner to the lower left corner of the matrix.
Input format:

The first line of input gives a positive integer n (1<n≤10); the following n lines give n integers in each line, separated by spaces.
Output format:

gives the sum of all elements of this matrix in one row except the subdiagonal, last column, and last row.
Input example:

4
2 3 4 1
5 6 1 1
7 1 8 1
1 1 1 1

Output sample:

35

#include <stdio.h>
int main()
{
    
    
	int n;scanf("%d",&n);
	int a[n][n];
	for(int i=0;i<n;i++)
	{
    
    
		for(int j=0;j<n;j++)
		{
    
    
			scanf("%d",&a[i][j]);
		}
	}
	
	
	int sum=0;
	for(int i=0;i<n;i++)
	{
    
    
		for(int j=0;j<n;j++)
		{
    
    
			sum+=a[i][j];
			if(i+j==n-1||i==n-1||j==n-1)
			{
    
    
				sum-=a[i][j];
			}
		}
	}
	printf("%d",sum);
}

7-2 Square matrix circularly moves to the right

This question requires writing a program to cyclically move each element in a given n×n square matrix m positions to the right, that is, transform the 0th, 1st, ⋯, n−1 columns into the n−m, n−m+1, ⋯, n−1, 0, 1, ⋯, n−m−1 columns.
Input format:

The first line of input gives two positive integers m and n (1≤n≤6). There are a total of n lines next, each line contains n integers, representing a square matrix of order n.
Output format:

Output the shifted square matrix according to the input format: that is, output n lines, each line has n integers, and output a space after each integer.
Input example:

2 3
1 2 3
4 5 6
7 8 9

Output sample:

2 3 1
5 6 4
8 9 7

#include <stdio.h>
int main()
{
    
    
	int m,n;scanf("%d %d",&m,&n);
	int a[n][n],b[n][n];
	for(int i=0;i<n;i++)
	{
    
    
		for(int j=0;j<n;j++)
		{
    
    
			scanf("%d",&a[i][j]);
			b[i][j]=a[i][j];
		}
	}

	for(int i=0;i<n;i++)
	{
    
    
		for(int j=0;j<n;j++)
		{
    
    //使用两个数组,因为单个数组实现不了元素的循环
			b[i][(j+m)%n]=a[i][j];			
		}
	}
	
	
		for(int i=0;i<n;i++)
		{
    
    
			for(int j=0;j<n;j++)
			{
    
    
				printf("%d ",b[i][j]);
			}
			printf("\n");
		}
}

7-3 Spiral Square

The so-called "spiral square matrix" means that for any given N, numbers from 1 to N×N are filled in N×N in a clockwise spiral direction starting from the first grid in the upper left corner. In the square array. This question requires the construction of such a spiral square matrix.
Input format:

The input gives a positive integer N (<10) in one line.
Output format:

Output N×N spiral square matrix. There are N numbers in each line, and each number occupies 3 digits.
Input example:

5

Output sample:

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

#include <stdio.h>
int main()
{
    
    
	int n;scanf("%d",&n);
	int a[n][n];
	int t=1;//最开始填入的数字位1 此后自增
	int startrow=0,endrow=n-1,startcol=0,endcol=n-1;//表示行列
	
	while(t<=n*n)
	{
    
    
		//第一行从左至右
		for(int j=startcol;j<=endcol;j++)
		{
    
    
			a[startrow][j]=t++;
		}
		
		startrow++;
		
		//最后一列从上到下
		for(int i=startrow;i<=endrow;i++)
		{
    
    
			a[i][endcol]=t++;
		}
		
		endcol--;
		
		//最后一行从右到左
		for(int j=endcol;j>=startcol;j--)
		{
    
    
			a[endrow][j]=t++;
		}
		
		endrow--;
		
		//第一列从下到上
		for(int i=endrow;i>=startrow;i--)
		{
    
    
			a[i][startcol]=t++;
		}
		
		startcol++;
		//由此开始新的螺旋方阵
		
	}
	
	
	for(int i=0;i<n;i++)
	{
    
    
		for(int j=0;j<n;j++)
		{
    
    
			printf("%3d",a[i][j]);
		}
		printf("\n");
	}
}

7-4 Array-Yang Hui Triangle

Input a positive integer N and output the first N rows of Yang Hui's triangle.
Input format:

Enter a positive integer N (N<=20).
Output format:

Output the first N lines of Yang Hui's triangle. The width of each number is 7. If the number of data digits is less than 7, spaces will be added to the left end and there will be no spaces at the end of the line.
Input example:

4

Output sample:

  1
  1      1
  1      2      1
  1      3      3      1
#include <stdio.h>
int main()
{
    
    
	int n;scanf("%d",&n);int a[n][n];
	
	for(int i=0;i<n;i++)
	{
    
    
		for(int j=0;j<=i;j++)
		{
    
    
			if(j==0||j==i)
			{
    
    
				a[i][j]=1;
			}
			else
			a[i][j]=a[i-1][j]+a[i-1][j-1];
		}
	}



    for(int i=0;i<n;i++)
    {
    
    
		for(int j=0;j<=i;j++)
		{
    
    
			printf("%7d",a[i][j]);
		}
		printf("\n");		
	}
	
	
}

7-5 Array-Diagonal sum

Find the sum of the positive diagonal and anti-diagonal elements of an N*N integer matrix.
Input format:

In the first line, enter a positive integer N (N<=100);

Next, enter N lines, each line contains N integers (all satisfying the int range).
Output format:

The output is only one line, including two positive integers separated by spaces, which respectively represent the sum of the positive diagonal elements and the sum of the anti-diagonal elements of the matrix.
Input example:

3
2 4 3
1 3 5
4 1 3

Output sample:

8 10

#include <stdio.h>
int main()
{
    
    
	int n;scanf("%d",&n);int a[n][n];
	int all=0,sum=0;
	
	for(int i=0;i<n;i++)
	{
    
    
		for(int j=0;j<n;j++)
		{
    
    
			scanf("%d",&a[i][j]);
			if(i+j==n-1)
			all+=a[i][j];
			if(i==j)
			sum+=a[i][j];
		}
	}

	printf("%d %d",sum,all);
}

7-6 Array-Matrix minimum value

Given a matrix of size M*N, please find the minimum value and its coordinates of the matrix.
Input format:

The first line inputs two integers M, N (3 <= M, N <= 20);

The next M rows have N integers in each row, which are the elements of the matrix. Two integers on each line are separated by spaces.
Output format:

The output is just one line, containing three integers. The first integer represents the value of the smallest element of the matrix, and the second and third integers represent the row and column numbers of the element respectively.

The data guarantees that the minimum value exists and is unique. Row numbers and column numbers start counting from 1.

The two numbers are separated by a space, and there are no extra spaces at the end of the line.
Input example:

3 3
1 2 3
1 -2 4
1 2 3

Output sample:

-2 2 2

#include <stdio.h>
int main()
{
    
    
	int m,n;scanf("%d%d",&m,&n);int a[m][n];
	int min,b,c;
	
	for(int i=0;i<m;i++)
	{
    
    
		for(int j=0;j<n;j++)
		{
    
    
			scanf("%d",&a[i][j]);
		}
	}

    min=a[0][0];
    
	for(int i=0;i<m;i++)
	{
    
    
		for(int j=0;j<n;j++)
		{
    
    
		    if(a[i][j]<min)
			{
    
    
				min=a[i][j];
				b=i,c=j;
			}
		}
	}
			
	printf("%d %d %d",min,b+1,c+1);
}

Guess you like

Origin blog.csdn.net/2301_77485708/article/details/134442033