我遇到了这个术语 - 奎因(也称为自我复制程序).只是想了解更多信息.如何写一个quine,他们在任何地方使用,或者他们只是一个有趣的运动?
我从Python开始,我可能会尝试用Python编写一个.有什么建议?
Alg*_*ias 28
奎因在实际意义上是无用的,但它们是一个很好的练习,可以帮助你更多地了解一门语言.
这是python中非常简洁的一个:
a='a=%r;print a%%a';print a%a
Run Code Online (Sandbox Code Playgroud)
Mar*_*iek 10
quine是一个计算机程序,它将自己的源代码副本作为唯一的输出.
我还没有看到一个实际用途,但我确定那里有一个.
print (lambda s:s+`s`+')')("print (lambda s:s+`s`+')')(")
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>
int main(int argc, char** argv)
{
/* This macro B will expand to its argument, followed by a printf
command that prints the macro invocation as a literal string */
#define B(x) x; printf(" B(" #x ")\n");
/* This macro A will expand to a printf command that prints the
macro invocation, followed by the macro argument itself. */
#define A(x) printf(" A(" #x ")\n"); x;
/* Now we call B on the text of the program
up to this point. It will execute the command, and then cause
itself to be printed. */
B(printf("#include <stdio.h>\n\nint main(int argc, char** argv)\n{\n/*
This macro B will expand to its argument, followed by a printf\n
command that prints the macro invocation as a literal string
*/\n#define B(x) x; printf(\" B(\" #x \")\\n\");\n\n/* This macro
A will expand to a printf command that prints the\n
macro invocation, followed by the macro argument itself. */\n#define A(x)
printf(\" A(\" #x \")\\n\"); x;\n\n/* Now we call B on the text
of the program\n up to this point. It will execute the command,
and then cause\n itself to be printed. */\n"))
A(printf("/* Lastly, we call A on a command to print the remainder
of the program;\n it will cause itself to be printed, and then
execute the command. */\n}\n"))
/* Lastly, we call A on a command to print the remainder of the program;
it will cause itself to be printed, and then execute the command. */
}
Run Code Online (Sandbox Code Playgroud)
正如其他人所解释的那样,quines是可以复制自己的精确副本的程序.
关于应用程序,如果你认为DNA编码逻辑来解释自身并重现自己 - 答案是非常简单的,没有quines的概念我们就不会在这里,我们将永远无法创造人工(自我复制) )生活.
这是我最喜欢的C例子
char*p="char*p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
Run Code Online (Sandbox Code Playgroud)
我从中学到了两件事: