The 11th Blue Bridge Cup-Serpentine Filling the Numbers

Problem description
As shown in the figure below, Xiaoming fills an infinite matrix with a "snake shape" of positive integers starting from 1.
Insert picture description here

It is easy to see that the number in the second row and second column of the matrix is ​​5. Could you please calculate the number in the 20th row and 20th column of the matrix?

Answer submission
This is a question that fills in the blanks with the result. You only need to calculate the result and submit it.
The result of this question is an integer. Only fill in this integer when submitting the answer, and fill in the extra content will not be scored.


Answer: 761


Problem solution one
Find the law:

解题思路: The numbers on the diagonal are 1, 5, 13, 25...

#include <iostream>
using namespace std;

int main()
{
    
    
	int w = 4, ans = 1;
	for (int i = 1; i <= 19; i ++)
	{
    
    
		ans += w;
		w += 4;
	}
	
	cout << ans << endl;
	return 0;		
}

Problem solution two
Find the law:

解题思路

  • The beginning of the first layer is 1, the coordinates are (1, 1), and the sum of the rows and columns is 2;
  • The beginning of the second layer is 2, the coordinates are (2, 1), and the sum of the rows and columns is 3;
  • The beginning of the third layer is 4, the coordinates are (3, 1), and the sum of the rows and columns is 4;
  • The beginning of the fourth layer is 7, the coordinates are (1, 4), and the sum of the rows and columns is 5;
#include <cstdio>
#include <iostream>
using namespace std;

int g[40][40];

int main()
{
    
    
	int k = 1;
	for (int i = 2; i <= 40; i ++)					// 枚举行列之和 
		if(i % 2 == 0)
		{
    
    
			for (int j = 1; j < i; j ++)			// 奇数层:从下到上 
				g[j][i - j] = k ++;
		}
		else
		{
    
    
			for (int j = i - 1; j >= 1; j --)		// 偶数层:从上到下 
				g[j][i - j] = k ++;
		}
		
	for (int i = 1; i <= 20; i ++)
	{
    
    
		for (int j = 1; j <= 20; j ++) printf("%4d", g[i][j]);
		cout << endl;
	}
			
	return 0;		
}

Lanqiao Cup C/C++ Group Provincial Competition Past Years Questions

Guess you like

Origin blog.csdn.net/weixin_46239370/article/details/115044476