对角线数字模式逻辑

时间:2017-09-22 02:54:40

标签: algorithm logic

我想设置算法来显示以下模式:

1  
9 2  
10 8 3  
14 11 7 4  
15 13 12 6 5  

有没有办法将其转换为以下数组并使用上述矩阵的索引并找出数组中数字的位置: 1 9 2 10 8 3 ...

我找不到使用矩阵坐标计算元素的模式,这就是为什么我试图设置上述方法以某种方式确定数组中下一个数字的位置。

1 个答案:

答案 0 :(得分:1)

只是线索:

你需要知道算术级数之和的公式(这里是自然数之和1 + 2 + 3 +.. N

1st step: determine number of diagonal where k-th item of array stands. 
2nd step: get direction of this diagonal filling
3rd step: get number of place at this diagonal
4th step: find what number stands here