跳到正文

个人记录

Quine 题解

Lucas2011xy约 1 分钟
原文
文章信息与版本

文章信息

发布
2023年4月8日
收录
2025年12月12日
更新
2026年11月22日
篇幅
约 1 分钟

版本

  1. v2当前
  2. v1

Quine ,毒瘤题目:

代码
写一个程序,使其能输出自己的源代码。
代码中必须至少包含十个可见字符。

可以这么做:print(print(print(...))) 好吧,离谱…


其实,这题的关键在于:如何防止“套代码”! 那就把代码存在字符串里: s="s={0};print(s.format(s))";print(s.format(s)) 结果: s=s={0};print(s.format(s));print(s.format(s)) 少了俩引号!


然后: s="s={0};x={1};print(s.format(x+s+x,x))";x='"';print(s.format(x+s+x,x)) 结果: s="s={0};x={1};print(s.format(x+s+x,x))";x=";print(s.format(x+s+x,x)) 又少了俩引号!


再来一个: s="s={0};x={1};y={2};print(s.format(x+s+x,y+x+y,x+y+x))";x='"';y="'";print(s.format(x+s+x,y+x+y,x+y+x)) (互帮互助?) 居然A\textcolor{green}{A}了!

原文