我们大多数失去的粉丝还记得电脑,德斯蒙德必须在每108分钟输入一个字符"4 8 15 16 23 42“,否则世界就会结束(或者会吗?)
这里的挑战是创建一个程序,通过要求每108秒输入一次输入4 8 15 16 23 42
,否则它将显示消息,从而完成相同的任务。
Sorry, the world has ended with status code -1
它应该在100秒内警告用户,他们需要输入一个带有消息的数字。
Enter, Quick!
程序必须能够在任何时候读取输入,如果是正确的输入,它将重置计时器。如果输入不正确,什么都不会发生。
程序应该无限期地运行。因此,最后一个有效输入后的时间线如下所示
从0到99秒:没有输出
100秒:Enter, Quick!
108秒:Sorry, the world has ended with status code -1
。
这是代码高尔夫,所以完成这项任务的最短答案(以字节为单位)获胜!祝好运!
发布于 2015-09-18 05:27:54
我错误地记得Java有一个nextLine方法,这个方法需要争论等待多长时间,所以我认为这很容易。我找不到这样做的方法,所以我用两个线程实现了它。有点笨重。哦,好吧。
t=Thread.start{while(1)try{Thread.sleep(1e5);println "Enter, Quick!";Thread.sleep(8e3);println "Sorry, the world has ended with status code -1";System.exit(-1)}catch(e){}};while(1)if(System.console().readLine()=="4 8 15 16 23 42")t.interrupt()
这假设世界末日的正确行为是进程退出,状态代码为-1。如果预期的行为是保持循环,并期望外部力量结束世界(通过扩展,程序),则可以省略;System.exit(-1)
部分以节省16个字节。耶。
我最初写这篇文章是为了使用字符串的hashCode,但是这比嵌入字符串的确切比较要长得多,因为hashCode
很长。
发布于 2019-07-09 09:40:15
在Linux上编译需要-pthread
开关。MinGW不需要。
#import<iostream>
#import<thread>
using namespace std;auto N=chrono::steady_clock::now;auto L=N();int w;int main(){thread A([]{for(;;){auto t=chrono::duration_cast<chrono::seconds>(N()-L).count();t>99&&!w?puts("Enter, Quick!"),w=1:t>107?exit(puts("Sorry, the world has ended with status code -1")),0:0;}}),B([]{for(string s;;s=="4 8 15 16 23 42"?L=N(),w=0:0)getline(cin,s);});A.join();B.join();}
https://codegolf.stackexchange.com/questions/58181
复制相似问题