晚上参加某公司的笔试题遇到如下一道题,本来打算在网上搜索,无奈全是英文,所以我将大概意思写出,各位有知道是什么问题的可以告诉我,比如说这是道费波那奇数列,我想了半天没想出这道题该如何求解。
题目如下:
给你一个数比如 6,从0开始,第n步可以走n个距离
比如 第一步(0-->1) 走了1步
第二步(1-->3) 走了2步
第三步(3-->6) 走了3步
当然你也可以往负数那边走
比如4的话
(0,-1) (-1,1) (1,4)
走1步 走2步 走三步
程序C/C++ 实现不能使用任何stl,所有实现自己写,要求给定数字N,求出其所走序列。
表达能力有限,如果有疑问在此留言。
楼下几个说简单的,你不妨写出代码,多试几个数字,就知道这个程序有多简单了。
2015.9.21 19:59
………………………………………………………………………………
The solution I think of is a bit inefficient, but at least the idea is feasible.
I don’t know C/C++, so I’ll just write my thoughts to you.
You have 2 states for each step, forward or backward. You can draw a tree, which looks like this.
The nth level represents the possible location of the nth step. You will find that the numbers on the left and right sides of each level are opposite. For example, the third level, -3,1 and -1,3. So for the third level, you only need to save 1 and 3.
Since level n can take n-1 steps, all situations at level n are plus or minus all situations at level n-1 plus or minus n.
For example, the third level, since the second level has 1 and 3, the third level is 1+3, 1-3, 3+3, 3-3, which is 4, -2, 6, 0, all The situation is -6, -4, -2, 0, 2, 4, 6. But we only store the absolute values, which are 0, 2, 4, and 6.
You just count down one level at a time, and see if the absolute value of the result is in it at each level. If it is there, it means that the result is found, and then the path is generated by working backwards.
The method of reverse reasoning is that for the selected number x at the nth level, look at the number |x+n| or |x-n| in the n-1th level.
For example, the number we are looking for is 4, and it is found at the third level of the tree above. We calculate 4+3 and 4-3, which is 7 and 1. If there is |1| in the second level, we select 1 in the second level. Then calculate 1+2, 1-2, and get 3 and -1. There is |-1| in level 1, so we select -1 in level 1 (although we only record the absolute value, negative values still exist in this level). Then the path came out.
(0,-1)->(-1,1)->(1,4)
Maybe I don’t understand it, but I think this can be searched directly.
For the first time, assume that you go all the way to the right, and then use a sequence to store all the coordinates. After reaching the end point, you want to output the path;
For the second time, assume that you go right for the first n-1 steps and the last step Go left, and the coordinates after the end point are output in reverse;
The third time assumes that the first n-2 steps go to the right, the n-1 step goes to the left, and the n-2 step goes to the right;
the fourth time Assume that the first n-2 steps go to the right, and the last two steps go to the left;
...
and so on, until the last round, just go left.
Every step has two directions, just run them all. There are 2^n options in total.
The code is as follows:
Someone said above that the search state is represented by a full binary tree. Then the data structure is obvious, a
vector
. You can refer to the implementation of the heap. The first level index is 0, and the second level is 1, 2 ... The search is obviously a bsf. After finding it, it is easy to reverse the path (parent function in the heap). overChallenge accepted.
Result:
I don’t know C++, so you just have to read it, that’s about it.