hdu1030 Pyramid square number to find the number of layers math problem

I have been thinking about this question for a long time and have no ideas. In fact, it can be seen that it is related to height. I may think that it is more troublesome to calculate the height. After thinking about it for a while, the thinking is broken there. Now I know the importance of the foundation. Some topics may be the first Once you know how to think, it will be much easier


I have read the code, the idea is still very clear, the side that crosses is the height of the floor, the difference between the height of the left floor and the height of the right floor, as shown in the figure below, (the code will be posted tomorrow when you have time...)

life

right_n

left_n

 

In fact, in comparison, how to find the height of the floor is to finish this question, I have gained the most, let me summarize it below

leve 1:1->1

leve 2:3 ->4

leve 3:5 ->9

leve 5:7 ->16

...

It is easy to find that the total number of each layer is actually a square number, then leve_n= sqrt(n-1)+1, you should know why when you see this formula, I didn’t expect it at the beginning, it’s so clever, I thought it was going to set a layer of loops To traverse, or too young

Then, we can use leve_n to push right_n, right_n can be regarded as the right number, right_n = ((leve_n*leve_n)-n)/2+1

Similarly, right_n = (leve_(n-1)*leve_(n-1)-n+1)/2


 I always feel that the square number will be encountered after finding the number of layers, so set up a flag first

Reprinted in: https://www.cnblogs.com/EdsonLin/p/5335506.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326409955&siteId=291194637