等等...我又要说什么?


10

当老师叫我说话时,我常常忘记我想说的话。您可以让我成为解决此问题的工具吗?

要求:

  • 该程序必须如下循环:
    • 首先,输入
    • 如果输入为空,则打印最后存储的问题。
    • 否则,将输入存储为问题。

笔记:

  • 如果没有问题存储,输入将永远不会为空。

澄清:该程序不需要存储多个问题。 我最初希望它存储多个,但是在许多人误解了这个问题之后,我进行了更改。

请记住,这是,所以字节数最少的代码将获胜。


您是说“先前存储的问题”吗?我发现“最后存储的问题”令人困惑,因为它表明您可能会存储多个问题。
尼尔

1
@Neil应该存储多个问题,但是由于许多答案已经误解了这个问题,因此我将其保留。
奥利弗·倪

Answers:


10

Perl,13 17 14 +1 = 15个字节

/./?$;=$_:say$

-n标志+1 (通过@Dada节省了4个字节)

只要输入不等于回车,它将输入存储在中$;。如果它等于回车符,则会打印出已存在的内容$;

假设所有输入都可以被描述为问题,即使在语法上也并非如此。


您可能想要写ne而不是eq(现在不起作用)。
达达

1
您可以通过执行/./而不是$/ne$_;-) 来节省3个字节
达达州

8

05AB1E,12 11 10字节

感谢@daHugLenny和@Adnan 1个字节!
感谢@Emigna 1个字节!

[IDõQiX,ëU

1
我现在无法测试,但我认为您可以删除最后一个字符。
acrolith

1
是的,您可以删除最后一个括号(它将自动完成)。
阿德南

2
如果用和替换®和,则不需要反斜杠。©XU
Emigna '16

4

Haskell,49个字节

g s=getLine>>=(#s)
""#s=putStr s>>g s
l#s=g l
g""

工作原理:从开始g ""s函数的参数g是存储中的问题。g从stdin读取下一行并将其(以及s)传递给#。如果该行为空,则#打印商店并g再次调用。如果该行不为空,g则将该行作为新存储被调用。


3

JavaScript, 36 34 31个字节

for(;;b?a=b:alert(a))b=prompt()

无限循环不断要求输入并将其存储在中b。如果输入不为空,则将其存储在中a,否则将输出的值a

@ETHproductions节省了2个字节


好答案。您可以通过删除来保存一个字节1,并通过移至b?a=b:alert(a)for循环的最后一部分(即for(a=0;;b?a=b:alert(a))b=prompt())保存一个字节
ETHproductions 2016年

实际上a=0也没有必要。由于只有在存储了问题的情况下,输入才可以为空,因此,a如果未分配输入,则不会引用该输入。
Oriol

也使用firefox-49.0.2停止浏览器几秒钟
bunyaCloven16年

1
问题中的唯一要求是空输入将打印最后存储的问题。为什么要我保留对先前存储的问题的引用?
Oriol

1
@bunyaCloven对我来说,只有在告诉我它阻止对话框时,它才会停止浏览器。这是由于无限循环造成的。几秒钟后,浏览器检测到问题并杀死了JS。
Oriol

2

Mathematica,44个字节

If[(b=InputString[])=="",Print@a,a=b]~Do~∞

完整程序。的Unicode字符为U + 221E INFINITY \[Infinity]



1

网状,12字节

id""E[$dp]~*

在线尝试!

说明

id""E[$dp]~*
i             take a line of input     [input]
 d""E         push equality with ""    [input, input == ""]
     [$dp]    push that func           [input, input == "", [$p]]
         ~*   execute it iff equal
              on equal:
      $       drop empty input
       dp     duplicate then print the TOS
              this wraps around to the beginning, taking another line of input


1

Python3-49字节

s=""
while 1:
 b=input()
 if b:s=b
 else:print(s)

1
NameError: name 'b' is not defined
shooqie

@shooqie啊,我用手机打了这个,所以我不小心遗漏了b=input()。谢谢!
Yytsi '16

1

Python 3,34个字节

s=""
while[print(s)]:s=input()or s

不是吗 对我来说效果很好
蓝色

3
规范不要求存储多个问题?
蓝色

1

Python 3,60个字节

q=[]
while 1:
 s=input()
 if''<s:q+=[s]
 else:print(q.pop())

0

CMD,37个字节

可悲的set是不接受空输入:(

set/p a=
if %a%.=. echo b
set b=%a%
c

在文件名c.cmd

set/p a=
if %a%.=. echo b
set b=%a%
%0

在任何文件中。工作中(-用作空):

set/p a=
if %a%.=-. echo b
set b=%a%
c

set/p永远不会使变量为空,因此至少set a=在第一行中需要;我也想你的意思set b=%a%
尼尔

0

APLX,19个字节

a←⍞⋄→(0=⍴a)/1⋄a⋄→1

说明:

⍝ Note: the ⋄ symbols are statement separators, so I've replaced them here with newlines

a←⍞           ⍝ get an input string (⍞) and store it in variable a
→(0=⍴a)/1     ⍝ if the length of a (⍴a) is 0, goto (→) the start of this program (1)
a             ⍝ else, print a
→1            ⍝ goto the start of the program (1)

0

C,73 63字节

g(){char a[99],x;for(;;){gets(a);if(*a)x=*a;else*a=x,puts(a);}}

仅使用1个缓冲区和一个char用于处理逻辑。如果用户未输入任何内容,则只有缓冲区中的第一个字符被覆盖'\0',在这种情况下,它将被恢复x并打印,否则将第一个字母复制到x以后使用。

先前的代码:

main(){char a[99],b[99];for(;;){gets(a);if(*a)strcpy(b,a);else puts(b);}}

0

Java 7,189 187 178字节..

class M{public static void main(String[]a){String s="",x;do{x=new java.util.Scanner(System.in).nextLine();if(x.isEmpty())Syste‌​m.out.println(s);els‌​e s=x;}while(1>0);}}

通过@cliffroot可以new java.util.Scanner直接使用-9个字节。

可能是我第一次do-while在代码高尔夫挑战赛中使用a 。

当允许使用函数而不是程序时,它为135个字节(仍然比大多数其他答案高很多倍。)


这有点短,似乎可以正常工作–void c(){for(String s="",x;(x=new java.util.Scanner(System.in).nextLine())!=null;)if(x.isEmpty())System.out.println(s);else s=x;}
悬崖根

@cliffroot嗯。.您不是想念a for还是while某处吗?..什么是(x=new java.util.Scanner(System.in).nextLine())!=null;)
凯文·克鲁伊森

1
是的,我设法以某种方式错过了之前的时间:)应该是这样void c(){for(String s="",x;for(x=new java.util.Scanner(System.in).nextLine())!=null;)if(x.isEmpty())System.out.println(s);else s=x;},实际上比它的版本长了1个字节do-whilevoid c(){String s="",x;do{x=new java.util.Scanner(System.in).nextLine();if(x.isEmpty())System.out.println(s);else s=x;}while(1>0);}
悬崖根

@cliffroot当然是啊..谢谢!
凯文·克鲁伊森

0

sed,10个字节

/./{x;d};g

说明:

      (implicit at start of program) read input, overwriting old input
/./{  If the input is nonempty
  x;  swap the input with the hold space
  d   then restart, without any implicit printing of input
};    end if statement
g     Append the hold space to the [empty] input
      (implicit at end of program) print the input, then restart

很高兴看到一种外在的(即“非深奥的”)语言与某些高尔夫语言联系在一起!

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.