您编写的代码接受字符串文字 s 并打印它,同时替换特殊子字符串的出现(或 出现) s 中的 foo 是 s 本身的值。
到目前为止,您获取了程序的整个源代码,并将其用作 s 的定义。但是您从字符串中排除了s定义,而是将其替换为foo。
这就是总体思路。剩下的就是字符串格式化细节,真的。
Yes. A programme that can make a copy of itself is called a "quine".
The basic idea of most quines is:
You write code that takes a string literal s and prints it, while replacing occurrences (or the occurrence) of a special substring foo in s by the value of s itself.
You take the entire source code of the program so far and use it as the definition for s. but you exclude the definition ofsfrom the string, instead replacing it by foo.
That's the general idea. The rest is string formatting details, really.
A quine is a computer program which takes no input and produces a copy of its own source code as its only output. The standard terms for these programs in the computability theory and computer science literature are self-replicating programs, self-reproducing programs, and self-copying programs.
A quine is a fixed point of an execution environment, when the execution environment is viewed as a function. Quines are possible in any Turing complete programming language, as a direct consequence of Kleene's recursion theorem. For amusement, programmers sometimes attempt to develop the shortest possible quine in any given programming language.
Beyond the existence of specific quines, an important result in computability theory is that for any function you might want to compute, there exists a program that "knows its own program text", i.e. that could print itself if desired. This theorem is called Kleene's second recursion theorem.
I can make a modification of this language so that the following program prints "Hello, world!\n":
Hello, world!
So that's the program that prints itself.
Oh, you feel something strange about it, while it has a precise and correct mathematical definition? That's your problem. "I won't accept..." ha! Mathematics does accept, and she's the mistress I serve, so I post this answer.
I assume you allow interpreted languages. (At some level, all languages are interpreted.) Somebody writes the interpreter, and if you are writing it, you can add to it any built-in functions you like, such as a (lispy) function (foo) that does nothing except print "(foo)".
Or you can add a more complex macro-type function (printMeAndMyArgs ...).
It is possible in Java, but with some constraints.
I have written a simple code in java which prints itself. You can use literals of C/C++ to use the same program. You can add anything you want inside this program, it will print itself completely.
Conditions
Java file should not be inside any package
Folder structure should not contain any folders with spaces in its name
Compilation target should be default or same folder where java file resides
import java.io.FileInputStream;
import java.net.URL;
public class PrintYourself {
public static void main(String[] args) {
// TODO Auto-generated method stub
URL location = PrintYourself.class.getProtectionDomain().getCodeSource().getLocation();
String path=location.getFile();
path=path.replace("/bin", "/src");
System.out.println(path);
try{
FileInputStream st=new FileInputStream(path+"PrintYourself.java");
int i=0;
while((i=st.read())!=-1){
System.out.print((char)i);
}
st.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
file = open("temp1.py", "r")
try:
temp_python = open("temp1.py", "w")
except FileNotFoundError:
temp_python = open("temp1.py", "w")
for i in file:
temp_python.writelines(i)
// note this is python code
// name the file as temp1.py
u can use this code
file = open("temp1.py", "r")
try:
temp_python = open("temp1.py", "w")
except FileNotFoundError:
temp_python = open("temp1.py", "w")
for i in file:
temp_python.writelines(i)
// note this is python code
// name the file as temp1.py
发布评论
评论(10)
它被称为quine,并且有一个网站收集它们。
It's called a quine, and there's a site that collects them.
是的。能够复制自身的程序称为“quine”。
大多数 quines 的基本思想是:
您编写的代码接受字符串文字
s
并打印它,同时替换特殊子字符串的出现(或 出现)s
中的 foo 是s
本身的值。到目前为止,您获取了程序的整个源代码,并将其用作
s
的定义。但是您从字符串中排除了s
定义,而是将其替换为foo。这就是总体思路。剩下的就是字符串格式化细节,真的。
Yes. A programme that can make a copy of itself is called a "quine".
The basic idea of most quines is:
You write code that takes a string literal
s
and prints it, while replacing occurrences (or the occurrence) of a special substring foo ins
by the value ofs
itself.You take the entire source code of the program so far and use it as the definition for
s
. but you exclude the definition ofs
from the string, instead replacing it by foo.That's the general idea. The rest is string formatting details, really.
这称为 Quine:
资料来源:维基百科
This is called a Quine:
Source: Wikipedia
这确实是一个经典问题!
除了存在特定的quines之外,可计算性理论的一个重要结果是对于< em>任何您可能想要计算的函数,都存在一个“知道自己的程序文本”的程序,即如果需要的话可以打印自己。该定理称为克莱恩第二递归定理。
This is indeed a classic question!
Beyond the existence of specific quines, an important result in computability theory is that for any function you might want to compute, there exists a program that "knows its own program text", i.e. that could print itself if desired. This theorem is called Kleene's second recursion theorem.
在 Jon Skeet 发明的语言中,以下运算符打印“你好,世界!\n”。
我可以对此语言进行修改,以便以下程序打印“Hello, world!\n”:
这就是打印自身的程序。
哦,虽然它有精确正确的数学定义,但你觉得有些奇怪吗?那是你的问题。 “我不接受……”哈!数学确实接受,而且她是我侍奉的情妇,所以我发布了这个答案。
In the language invented by Jon Skeet the following operator prints "Hello, world!\n".
I can make a modification of this language so that the following program prints "Hello, world!\n":
So that's the program that prints itself.
Oh, you feel something strange about it, while it has a precise and correct mathematical definition? That's your problem. "I won't accept..." ha! Mathematics does accept, and she's the mistress I serve, so I post this answer.
我假设您允许解释语言。 (在某种程度上,所有语言都是解释性的。)有人编写了解释器,如果您正在编写它,您可以向其中添加您喜欢的任何内置函数,例如 (lispy) 函数
(foo)< /code> 除了打印“
(foo)
”之外什么也不做。或者您可以添加更复杂的宏类型函数
(printMeAndMyArgs ...)
。所以关键在于如何定义问题。
I assume you allow interpreted languages. (At some level, all languages are interpreted.) Somebody writes the interpreter, and if you are writing it, you can add to it any built-in functions you like, such as a (lispy) function
(foo)
that does nothing except print "(foo)
".Or you can add a more complex macro-type function
(printMeAndMyArgs ...)
.So the trick is in how you define the problem.
如果你写一个奎因,请注意副本不会无限地写出自己的副本并最终占领世界。
If you write a quine, be careful that the copies don't also write copies of themselves ad infinitum and end up taking over the world.
在 Java 中这是可能的,但有一些限制。
我用java编写了一个简单的代码,它可以自行打印。
您可以使用 C/C++ 的文字来使用相同的程序。您可以在该程序中添加任何您想要的内容,它会完全打印出来。
条件
Java 文件不应位于任何包内
文件夹结构不应包含任何名称中带有空格的文件夹
编译目标应该是默认的或java文件所在的同一文件夹
It is possible in Java, but with some constraints.
I have written a simple code in java which prints itself.
You can use literals of C/C++ to use the same program. You can add anything you want inside this program, it will print itself completely.
Conditions
Java file should not be inside any package
Folder structure should not contain any folders with spaces in its name
Compilation target should be default or same folder where java file resides
你可以使用这个代码
u can use this code