【发布时间】:2014-02-26 11:12:26
【问题描述】:
这是算法书上的任务。
问题是我完全不知道从哪里开始!
Trace the following non-recursive algorithm to generate the binary reflexive
Gray code of order 4. Start with the n-bit string of all 0’s.
For i = 1, 2, ... 2^n-1, generate the i-th bit string by flipping bit b in the
previous bit string, where b is the position of the least significant 1 in the
binary representation of i.
所以我知道 1 位的格雷码应该是 0 1,2 位的格雷码应该是 00 01 11 10 等等。
很多问题
1) 我知道对于 n = 1 我可以从 0 1 开始吗?
2)“以全0的n位字符串开头”应该怎么理解?
3) “前一个位串”?哪个字符串是“前一个”?以前的意思是来自较低的 n 位? (例如对于 n=2,previous 是来自 n=1 的那个)?
4) 如果唯一的操作是翻转,我如何将 1 位字符串转换为 2 位字符串?
这让我很困惑。到目前为止,我理解的唯一“人类”方法是:从较低的 n 位获取集合,复制它们,反转第二个集合,将 0 添加到第一个集合中的每个元素,添加 1 做第二个集合中的每个元素。完成(例如:0 1 -> 0 1 | 0 1 -> 0 1 | 1 0 -> 00 01 | 11 10 -> 11 01 11 10 完成。
感谢您的帮助
【问题讨论】: