且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我的荷兰国旗算法出了什么问题?

更新时间:2023-02-26 20:08:38

猜测:

每次循环都不应该增加电流,因为它应该代表1和未知数之间的分区。例如,当你击中前2个时,你最终用另外2个交换它然后继续前进。后来2换成0的事实是偶然的。较小和当前之间的元素应始终为1并且会被破坏。

You should not be increasing current every time through the loop since that is supposed to represent the partition between the 1's and the unknowns. For example, when you hit the first 2 you end up swapping it with another 2 and then moving on. The fact that the 2 later gets swapped for a 0 is accidental. The elements between smaller and current should always be 1's and that is broken.

当前++应仅在磁带[current]!= 2的情况下完成。磁带[current] = 0时可以这样做,因为你没有改变[较小 - >当前] =仅1的条件。当磁带[current] = 1时移动它是可以的,因为它满足1-only条件。

current++ should only be done in the tape[current] != 2 case. It's ok to do it when tape[current] = 0 because you haven't changed the [smaller -> current] = 1-only condition. And it's ok to move it when tape[current] = 1 because that satisfies the 1-only condition.

...但我还没有尝试过。

...but I haven't tried it.