引用大哥


16

任务:让您的程序在运行时随机选择以下两个引号之一,并完全按照所示打印该引号。

1:

Under the spreading chestnut tree
I sold you and you sold me.
There lie they, and here lie we
Under the spreading chestnut tree.

2:

WAR IS PEACE.
FREEDOM IS SLAVERY.
IGNORANCE IS STRENGTH.

限制:字spreadingchestnutPEACEFREEDOM,和STRENGTH 必须出现在你的代码。以上两个引号中的任何单词都不能多次出现在您的代码中。

最短的解决方案获胜。


1
好吧,我选了第一个。诚实!xkcd.com/221
edc65,2014年


@MartinBüttner并不是很有趣,而是建议一个更明确的规则。第一次读书我真的很坚强,我可以选择一个随机的,然后开始工作。
edc65

@ edc65啊,我明白了,以前的措词是不同的。
马丁·恩德

Answers:


3

PHP 179

es1024答案的启发。

<?=strtr(rand()%2?"0
I 1 232 1 me.
T5 they,35 we
0.
":"WAR4PEACE.
FREEDOM4SLAVERY.
IGNORANCE4STRENGTH.
",["Under the spreading chestnut tree",sold,you," and "," IS ","here lie"]);

4

Perl(191184个字符)

$_=$$&1?"0
I12321me6T4they,34we
06":"WAR5PEACE6FREEDOM5SLAVERY6IGNORANCE5STRENGTH6";s/\d/("Under the spreading chestnut tree"," sold ",you," and ","here lie "," IS ",".
")[$&]/ge;print

感谢@ core1024提供有关打高尔夫球的技巧。


1
您可以用实际的新行替换6,删除引号"you"$$&1用作随机生成器:)
core1024 2014年

3

爪哇338

我无法在338以下找到它...

class B{public static void main(String[]a){String b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=".\n",i=" IS ";if(Math.random()>.5)System.out.print(b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+h);else System.out.print("WAR"+i+"PEACE"+h+"FREEDOM"+i+"SLAVERY"+h+"IGNORANCE"+i+"STRENGTH"+h);}}

有点不打高尔夫球:

class B{public static void main(String[]a){String b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=".\n",i=" IS ";
if(Math.random()>.5)System.out.print(b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+h);
else System.out.print("WAR"+i+"PEACE"+h+"FREEDOM"+i+"SLAVERY"+h+"IGNORANCE"+i+"STRENGTH"+h);}}


感谢您的链接!明天会去寻找有用的东西。
CoolWilly 2014年

@Fabinout我尝试了一些技巧,但是它们没有帮助。(java.io.PrintStream p=System.out,总共增加了15个字节static<T>void p(T o){System.out.print(o);},,增加了12个)。还有其他建议吗?
CoolWilly

使用静态块代替主声明
Fabinout 2014年

静态块* 310 CHAR *:类B {static {String b =“在正在传播的板栗树下”,c =“已售”,d =“您”,e =“和”,f =“我。\ nT”, g =“ here lie”,h =“。\ n”,i =“ IS”; if(Math.random()>。5)System.out.print(b +“ \ nI” + c + d + e + d + c + f + g +“ they,” + e + g +“ we \ n” + b + h); else System.out.print(“ WAR” + i +“ PEACE” + h +“ FREEDOM” + i +“ SLAVERY “ + h +” IGNORANCE“ + i +” STRENGTH“ + h);}}
Fabinout 2014年

2

JavaScript的229 254 256

new Date根据@Doorknob的建议使用1进行编辑 -我仍然不清楚JavaScript日期如何转换为字符串或整数或其他形式

编辑2简化。很多。

t=(new Date&1
   ?'0WAR0 IS 0PEACE0.\n0FREEDOM020SLAVERY040IGNORANCE020STRENGTH.'
   :'0Under the spreading chestnut tree0\nI 0sold 0you 0and 04030me.\nT0here lie 0they, 05090we\n010.'
  ).split(0);
for(i=o='';j=t[++i];)o+=t[j|0||i];alert(o)

new Date短于Date.now()
门把手

1
解释Date转换:> JavaScript日期基于时间值,该时间值是从UTC时间1970年1月1日午夜开始的毫秒数。一天可容纳86,400,000毫秒。相对于1970年1月1日UTC,JavaScript Date对象范围是-100,000,000天到100,000,000天。,当您&将其自动转换为时Number。因此,您基本上是根据自1970-01-01T00:00:00.000Z纪元以来的偶数或奇数毫秒做出随机选择。
鲍勃

@Doorknob您可以在codegolf.stackexchange.com/questions/2682/中为该较短的随机数生成器写一篇简短的文章吗?还没有看到它,我认为它可能非常有用。
瑕疵

@flawr 完成
门把手

2

C#(268)(260)(256)

这可以使用以下Dump()方法在LINQPad中执行:

string b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=" IS ";if(new Random().Next(9)>4)(b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+".").Dump();else("WAR"+h+"PEACE.\nFREEDOM"+h+"SLAVERY.\nIGNORANCE"+h+"STRENGTH.").Dump();

取消高尔夫:

string b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=" IS ";

if(new Random().Next(9)>4)
    (b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+".").Dump();
else
    ("WAR"+h+"PEACE.\nFREEDOM"+h+"SLAVERY.\nIGNORANCE"+h+"STRENGTH.").Dump();

更新:

使用三元运算符和1个额外的变量,我能够剪切另外6个字符:

string a,b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=" IS ";a=new Random().Next(9)>4?(b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+"."):("WAR"+h+"PEACE.\nFREEDOM"+h+"SLAVERY.\nIGNORANCE"+h+"STRENGTH.");a.Dump();

取消高尔夫:

string a,b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=" IS ";

a=new Random().Next(9)>4 ?
    (b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+".") :
    ("WAR"+h+"PEACE.\nFREEDOM"+h+"SLAVERY.\nIGNORANCE"+h+"STRENGTH.");

a.Dump();

更新2:

多亏了tsavinho我的巧妙建议,通过在三元操作周围放置花括号,我能够再节省4个字符:

string b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=" IS ";(new Random().Next(9)>4?(b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+"."):("WAR"+h+"PEACE.\nFREEDOM"+h+"SLAVERY.\nIGNORANCE"+h+"STRENGTH.")).Dump();

取消高尔夫:

string b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=" IS ";
(new Random().Next(9)>4?
    (b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+"."):
    ("WAR"+h+"PEACE.\nFREEDOM"+h+"SLAVERY.\nIGNORANCE"+h+"STRENGTH.")
).Dump();

1
您可以通过删除a并将其替换为这样的方括号来节省另外4个字符:string b =“在正在传播的栗树下”,c =“卖出”,d =“您”,e =“和”,f =“ me 。\ nT“,g =” here lie“,h =” IS“;(new Random()。Next(9)> 4?(b +” \ nI“ + c + d + e + d + c + f + g +“他们,” + e + g +“我们\ n” + b +“。”):(“ WAR” + h +“和平。\ nFREEDOM” + h +“奴隶制。\ nIGNORANCE” + h +“实力。”))。倾倒();
tsavinho 2014年

1

Python 3-228

for x in[b"AB:87078@624>049BA6",b"?;<61;=63;56"][id(id)%3-1]:print("and |FREEDOM|T|IGNORANCE|here lie |STRENGTH|.\n|you |sold |we|I | IS |PEACE|SLAVERY|they, |WAR|me|Under the spreading chestnut tree|\n".split("|")[x-48],end="")

略有偏差:

# id(id) returns an even number based on memory address of id
# id(id)%3-1 gives -1, 0 or 1
randomNumber = id(id)%3-1

# Word list
words = "and |FREEDOM|T|IGNORANCE|here lie |STRENGTH|.\n|you |sold |we|I | IS |PEACE|SLAVERY|they, |WAR|me|Under the spreading chestnut tree|\n".split("|")

# Byte Literals for Under the chestnut and WAR IS PEACE
# each byte correspond to the word's index in the word list
byteLiterals = [b"AB:87078@624>049BA6", b"?;<61;=63;56"]
choice = byteLiterals[randomNumber]

for x in choice: 
    print(words[x-48], end="") # Print each word out

1

PowerShell 205

("{0}.`nI{1}{2}{3}{2}{1} me.`nT{4}they,{3} {4}we`n{0}."-f"Under the spreading chestnut tree"," sold"," you"," and","here lie "),("WAR","PEACE.`nFREEDOM","SLAVERY.`nIGNORANCE","STRENGTH."-join" IS ")|Random

使用-f运算符将字符串放在第一个引号上,第二个由联接IS,之后,它选择这两个引号中的一个随机元素并将其打印出来...



1

JavaScript / jQuery 396

var B={
U:"Under the spreading chestnut tree",
s:"sold ",
y:"you ",
l:"lie ",
a:"and ",
i:"IS ",
b:"</br>"
};
var A={
P:B.U+B.b+"I "+B.s+B.y+B.a+B.y+B.s+"me."+B.b
+"There "+B.l+"they, "+B.a+"here "+B.l+"we."+B.b+B.U,
Q:"WAR "+B.i+"PEACE."+B.b+"FREEDOM "+B.i+"SLAVERY."+B.b
+"IGNORANCE "+B.i+"STRENGTH."
};
$(function(){
var z=(Math.random()<0.5)?A.P:A.Q; 
$('#d').append(z);
});

1

T-SQL, 337 327

只是为了好玩,我在T-SQL中提出了另一个解决方案,比我的Java 8解决方案短了一个字节:

DECLARE @b char(33)='Under the spreading chestnut tree',@c char(5)='sold',@d char(4)='you',@e char(4)='and',@f char(9)='here lie',@ char='
',@h char(4)=' IS'IF rand()>.5PRINT @b+@+'I '+@c+@d+@e+@d+@c+'me.'+@+'T'+@f+'they, '+@e+@f+'we'+@+@b+'.'ELSE PRINT'WAR'+@h+'PEACE.'+@+'FREEDOM'+@h+'SLAVERY.'+@+'IGNORANCE'+@h+'STRENGTH.'

通过将chars 声明太长,可以添加一个“自动”空间,节省几个字节。

使用PenutReaper用户建议的编辑中的代码,您确实可以减少10个字节。


使用一个变量作为回车符是不值得的。只需使用原义的。
Muqo 2014年

1

Java脚本(233)

Math.random()>0.5?(a="Under the spreading chestnut tree")+"\nI "+(s="sold")+(y=" you ")+(n="and")+y+s+" me.\nT"+(h="here ")+(l="lie ")+"they "+n+" "+h+l+"we\n"+a+".":["WAR","PEACE.\nFREEDOM","SLAVERY.\nIGNORANCE","STRENGTH."].join(" IS ")

撰写此内容时无需查看其他答案。充分利用赋值作为表达式。


0

05AB1E,119字节

“‚  spreading chestnutíÍ““Iïê€î€ƒ€îïê€á.““€Çº¤€»,€ƒ€Îº¤€¦“).ªĆ»‘‡î€ˆ PEACE.
FREEDOM€ˆÃÒRY.
IGNORANCE€ˆ STRENGTH‘)'.«Ω

在线尝试!


0

Python 3,222字节

a,b,c,d,e,f='Under the spreading chestnut tree',' you ',' lie ','and','sold',' IS '
print({f'{a}\nI {e}{b}{d}{b}{e} me.\nThere{c}they, {d} here{c}we\n{a}.',f'WAR{f}PEACE.\nFREEDOM{f}SLAVERY.\nIGNORANCE{f}STRENGTH.'}.pop())

在线尝试!


0

C(GCC) 307个 289 271 270字节

-37字节,感谢ceilingcat

*y=" IS ";main(){int*r[]={"WAR",y,"PEACE.\nFREEDOM",y,"SLAVERY.\nIGNORANCE",y,"STRENGTH.",0,"Under the spreading chestnut tree","\nI"," sold ","you"," and ",r[11],r[10],"me.\n","T","here lie ","they,",r[12],r[17],"we\n",r[8],".",0},z=r;for(z=z/2&8;r[z];)printf(r[z++]);}

在线尝试!

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.