Assignment rules for lower triangular arrays

This topic is the 2019 postgraduate media technology enrollment examination question of the School of Information Science and Technology of Northeast Normal University. It was provided by a high school student. It made me go back to my old career without using C for three years.

Topic content: Write a program to assign a value to the lower triangular element of the array x according to the data specified below, and output it in the following form:

                    4

                    3         7

                    2         6          9

                    1         5          8          10

Method 1: Find the law of Dafa

As the saying goes, it is the easiest to find the law, and it is easy to solve it with a general formula.

First observe each column, successively subtract 1 from top to bottom, use the macro definition level to indicate how many rows the array has, and use i and j to indicate the i-th row and j-th column (here i and j start from 0). Then the element in the ith row and the 0th column can be expressed as level-i .

Starting from the first column, the entire column can be seen as the 0th column increases by a certain value, the 1st column increases by 4 (level) compared to the 0th column, and the second column increases by 4+3=7(level+ level-1=2*level-1), the third column increases 4+3+2=9 compared to the 0th column (level+level-1+level-2=3*level-3)......

Then the first item of the item added in the jth column compared to the 0th column is j*level, and the second item is the sum of 0,1,2,3,...j-1, which is obtained by the general term formula. The binomial is j(j-1)/2, so the total increase is j*level+j(j-1)/2.

In summary, the expression can be written as num[i][j]=level-i+level*jj*(j-1)/2;

Method 2: Round Robin

The loop method is very simple. Each column starts from the bottom element and increases upward, and the bottom element of each column is one larger than the top element of the previous column, that is, all elements are from the bottom of the column. To the upper continuous.

That's fine. Assuming that the array has a total of level rows, and the lower triangular array has a total of level*(level+1)/2, then I first initialize the variable index to 0, which is used to indicate the number of assignments for each assignment, and initialize pi as level-1, initialize pj to 0, which means the current level-1 row and jth column.

Then enter the while loop, the loop condition is that the index is less than level*(level+1)/2, and if it is greater than, it will jump out.

In the loop, first assign num[pi][pj] to index+1 (because it starts from 1), then index increases automatically, and the following judgments are made: if pi==pj, that is, the current element is on the main diagonal, This means that when you reach the top of the column of the lower triangular array, you need to reinitialize pi to level-1, and pj will increase automatically, otherwise pi will decrease.

 

 

Code:

method one:

#include <stdio.h>
#define level 4
int main(){
	int num[level+1][level+1];
	for(int i=0;i<level;i++){
		for(int j=0;j<level;j++){
			if (i>=j){//下三角  
				num[i][j]=level-i+level*j-j*(j-1)/2;
				printf("%d",num[i][j]);
				if(i!=j){
					printf(" ");
				}
			}
		}
		printf("\n");
	}
}

Method Two:

#include <stdio.h>
#define level 4
int main(){
	int num[level+1][level+1];
	int number=level*(level+1)/2;
	int index=0;
	int pi=level-1;//假装我是指向行数的指针 
	int pj=0;//假装我是指向指向列数的指针 
	while(index<number){
		num[pi][pj]=index+1;//日常赋值 
		index++;
		if(pi==pj){//如果到主对角线 则跳转到下一列的末尾 
			pi=level-1;
			pj++;
		}
		else{//否则跳转到上一行 
			pi--;
		}
	}
		for(int i=0;i<level;i++){
		for(int j=0;j<level;j++){
			if (i>=j){//下三角  
				printf("%d",num[i][j]);
				if(i!=j){
					printf(" ");
				}
			}
		}
		printf("\n");
	}
	} 

 

Guess you like

Origin blog.csdn.net/qq_36614557/article/details/103467366