gpt4 book ai didi

quine - 写一个 Quine 的 "trick"是什么?

转载 作者:行者123 更新时间:2023-12-03 11:42:40 25 4
gpt4 key购买 nike

我读过 Ken Thompson 的经典论文 Reflections on Trusting Trust他在其中提示用户写一个 Quine作为他的论点的介绍(强烈推荐阅读)。

A quine is a computer program which takes no input and produces a copy of its own source code as its only output.



天真的做法只是想说:
print "[insert this program's source here]"

但是很快就会发现这是不可能的。我结束了 writing one myself使用 Python 但仍然无法解释“诀窍”。 我正在寻找一个很好的解释为什么 Quines 是可能的。

最佳答案

正常的技巧是使用 printf这样格式字符串代表程序的结构,并带有字符串本身的占位符以获得所需的递归:

来自 http://www.nyx.net/~gthompso/quine.htm 的标准 C 示例很好地说明了这一点:

char*f="char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c";main(){printf(f,34,f,34,10);}

编辑 : 写完之后,我搜索了一下: http://www.madore.org/~david/computers/quine.html对quines到底是什么以及它们为什么起作用给出了一个非常好的、更理论的描述。

关于quine - 写一个 Quine 的 "trick"是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8872964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com