如何在Floyd Triangle中找到数字所属的行和列 [英] How do you find which row and column a number belongs to in Floyd Triangle
问题描述
如何查找弗洛伊德三角区中数字所属的行和列?
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
How do you find which row and column does a number belongs to in Floyd Triangle?
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
例如,
- 33位于第8行第5列(输入33→输出第8行第5列)
- 46位于第十行第一列
- 27位于第七行第六列
非常感谢您!
推荐答案
请注意,第n行结尾为值 n*(n+1)/2
.因此,您可以使二次方程式求解并求解以获得给定数k的行数.
Note that n-th row ends with value n*(n+1)/2
. So you can make quadratic equation and solve it to get row number for given number k
n*(n+1)/2 = k
n^2 + n - 2*k = 0
D = 1 + 8*k
n_row = Ceil((-1 + Sqrt(D)) / 2) //round float value up
例如,对于k = 33,您可以计算
For example, for k=33 you can calculate
n_row = Ceil((-1 + Sqrt(265)) / 2) =
Ceil(7.639) =
8
具有n_row,找到前一行的最后一个数字和k在当前行中的位置
Having n_row, find the last number of previous row and position of k in the current row
n_Column = 33 - n_row * (n_row - 1) / 2 =
33 - 28 =
5
用于查找行的替代方法的伪代码:
Pseudocode for alternative method of row finding:
sum = 0
row = 0
while sum < k do
row++
sum = sum + row
这篇关于如何在Floyd Triangle中找到数字所属的行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!