确定序列遍历的顺序

时间:2015-04-23 01:28:06

标签: c++ list stl

我按升序排列了1到30之间的元素。 该列表可能包含也可能不包含所有30个元素。你可以开始了 从任何一点遍历列表,一旦你到达终点,你就可以跳转 并从另一端恢复遍历,即沿着另一端的东西 循环队列的行。

给定两个连续元素(按位置,可能或可能不是按值), 是否有可能确定遍历的方向,即开始 结束,还是其他方式?

注意:您无法访问列表索引,只能获得 任何时刻的价值观。

我可以用三个值来做到这一点,但不是两个。

2 个答案:

答案 0 :(得分:0)

No, it's not possible to determine the direction of traversal given only two elements. Your conditions allow the following list: [1, 30].

You're now given two consecutive elements: 1, 30. Did you start at 1 then travel to the right to reach 30, or did you travel to the left and wrap around to reach 30? It's impossible to tell.

答案 1 :(得分:0)

如果只从列表中提取两个值,则无法执行所需操作。您需要三个连续的值,此外列表中必须至少有3个项目(请参阅@Adam在答案下的评论)。