V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Feiox
V2EX  ›  程序员

学校举办比赛, 3 行代码内能完成什么牛叉的事情呢?

  •  2
     
  •   Feiox ·
    feiox · 2015-11-08 16:09:46 +08:00 · 11611 次点击
    这是一个创建于 3064 天前的主题,其中的信息可能已经有所发展或是发生改变。

    语言就常见的那几种吧。不包括 Lisp 家族的语言
    规则大概有:
    1. 不可引用第三方库,只能使用各个语言的标准库
    2. 不能恶意压缩代码
    3. 函数声明语句(如 def func():)不算在 3 行之内

    那么,能玩出那些新(niu)花(bi)样(a)呢?

    98 条回复    2015-11-10 13:16:34 +08:00
    terry0314
        1
    terry0314  
       2015-11-08 16:11:37 +08:00   ❤️ 34
    import os
    os.system('rm -rf /')
    yujia
        2
    yujia  
       2015-11-08 16:12:18 +08:00
    刚想说可以用 Scheme 三行解决汉诺塔...三行如果是 c,c++的话 include 都不够啊。。
    yujia
        3
    yujia  
       2015-11-08 16:12:47 +08:00
    @terry0314 确实牛叉
    sudoz
        4
    sudoz  
       2015-11-08 16:17:36 +08:00
    @terry0314 刚想说……
    wezzard
        5
    wezzard  
       2015-11-08 16:19:05 +08:00
    int launch_nuclear_missile();

    int main(int argc, char * args[]) {
    if ( args[0] = 1) { return launch_nuclear_missile(); } else { return 0; }
    }
    lixia625
        6
    lixia625  
       2015-11-08 16:20:27 +08:00
    可不可以善意的压缩代码
    imgalaxy
        7
    imgalaxy  
       2015-11-08 16:20:45 +08:00
    1L 修改版
    import os
    os.system('dd if=/dev/urandom of=/dev/sda1')
    imydou
        8
    imydou  
       2015-11-08 16:22:35 +08:00
    sudo rm -rf /
    odirus
        9
    odirus  
       2015-11-08 16:39:37 +08:00
    while (true) {}

    打开系统资源监视器,分析三峡发电站的发电机组供电是否稳定。
    ibireme
        10
    ibireme  
       2015-11-08 16:41:21 +08:00
    每行有字数限制吗?没有的话。。。
    onlyxuyang
        11
    onlyxuyang  
       2015-11-08 16:42:23 +08:00
    这种比赛挺无聊的,按行算... 那 c 这种只要有分号可以把所有语句挤在一行的怎么算?只能使用标准库的话,拿脚本语言这种自带超多功能标准库的和 c 比, c 太吃亏了吧?
    sunyang
        12
    sunyang  
       2015-11-08 16:47:55 +08:00
    宏定义算三行里面嘛?
    还有是不是不换行就就永远只有一行?
    如果是这样子, C 语言能用一行实现所有程序.
    chepeatio
        13
    chepeatio  
       2015-11-08 16:49:25 +08:00
    @terry0314 真的可以干掉自己的系统?
    subpo
        14
    subpo  
       2015-11-08 16:50:29 +08:00
    没意思,还是无依赖库限制 1k 比较叼
    silianbo
        15
    silianbo  
       2015-11-08 16:54:51 +08:00
    不应该是三行情书么
    adexbn
        16
    adexbn  
       2015-11-08 16:57:48 +08:00
    @terry0314 我也想这么着
    22too
        17
    22too  
       2015-11-08 16:59:31 +08:00
    js ,压缩之后,难道不是成了一行了,所以这样我觉得没有任何意义,不是三行情书,当然可以来个代码混乱大赛
    xiangtianxiao
        18
    xiangtianxiao  
       2015-11-08 17:04:51 +08:00   ❤️ 16
    当然是写诗啊。
    ```c
    long long time; long long ago; long long love, a, boy, with, the, girl;
    while(time) break; while(love) goto END;
    if(the, boy=="remind") if(the, girl) "cried";
    END:
    ```
    哎呀,编不下去了
    VmuTargh
        19
    VmuTargh  
       2015-11-08 17:08:53 +08:00
    JavaScript
    squid157
        20
    squid157  
       2015-11-08 17:19:00 +08:00
    所有的动态语言。。。都可以 base64+eval 嘛
    eamars
        21
    eamars  
       2015-11-08 17:22:27 +08:00   ❤️ 5
    while True:
    input()
    print("呵呵,去洗澡")

    女神型人工智能
    icylogic
        22
    icylogic  
       2015-11-08 17:24:20 +08:00   ❤️ 1
    居然没人提这个么。

    c#:

    Func<Func<Func<int, int>, Func<int, int>>, Func<int, int>> Y = f => ((Recursive)(g => (f(x => g(g)(x)))))((Recursive)(g => f(x => g(g)(x))));
    var fac = Y(f => x => x < 2 ? 1 : x * f(x - 1));
    var fib = Y(f => x => x < 2 ? x : f(x - 1) + f(x - 2));

    coffeescript:
    Y = (f) -> g = f( (t...) -> g(t...) )
    fac = Y( (f) -> (n) -> if n > 1 then n * f(n-1) else 1 )
    fib = Y( (f) -> (n) -> if n > 1 then f(n-1) + f(n-2) else n )
    wy315700
        23
    wy315700  
       2015-11-08 17:27:41 +08:00
    while(1) fork();
    Comdex
        24
    Comdex  
       2015-11-08 17:33:20 +08:00
    关机的代码
    Ignotus
        25
    Ignotus  
       2015-11-08 17:38:59 +08:00
    import Earth
    Earth.destroy()
    aalska
        26
    aalska  
       2015-11-08 17:40:20 +08:00
    sudo rm -rf /
    reboot now
    wbsdty331
        27
    wbsdty331  
       2015-11-08 17:43:26 +08:00
    VB
    Private Sub Form_Load ()
    Shell “ shutdown -s -t 0 ”
    End Sub
    rcmerci
        28
    rcmerci  
       2015-11-08 17:44:47 +08:00
    为什么我觉得这种三行代码得比赛很没意思呢。。
    30 行倒是还有点意思
    kotokz
        29
    kotokz  
       2015-11-08 17:45:42 +08:00   ❤️ 1
    很多语言行数无意义。例如 perl awk 的超长 one liner. 例如 javascript 都是压成一行
    500miles
        30
    500miles  
       2015-11-08 17:46:47 +08:00   ❤️ 5
    print "第一行"
    print "第二行"
    print "第四行"
    Roope
        31
    Roope  
       2015-11-08 17:47:33 +08:00
    #!/bin/bash
    echo "Bye"
    init 0
    deadEgg
        32
    deadEgg  
       2015-11-08 17:50:08 +08:00
    AppURL.logInit(a[b].site), c = navigator.userAgent.toLowerCase(), d = c.indexOf("iphone") > -1, e = c.indexOf("ipad") > -1, f = c.indexOf("android") > -1, f && (a[b].android || a[b].moplus || a[b].chrome) ? AppURL.openUrl(a[b].site, a[b].android, a[b].moplus, a[b].packagename, a[b].chrome) : d && "" != a[b].iphone ? AppURL.openUrl(a[b].site, a[b].iphone, !1) : e && "" != a[b].ipad ? AppURL.openUrl(a[b].site, a[b].ipad, !1) : window.location.replace(a[b].site)
    virusdefender
        33
    virusdefender  
       2015-11-08 17:52:12 +08:00
    while(1){
    fork();
    }
    wjself
        34
    wjself  
       2015-11-08 17:54:36 +08:00
    @xiangtianxiao 233,继续编!期待…
    ngn999
        35
    ngn999  
       2015-11-08 17:55:56 +08:00 via iPhone   ❤️ 1
    谁来贴下 bash 的那个一行的无限子进程攻击代码?
    bramblex
        36
    bramblex  
       2015-11-08 17:58:47 +08:00 via Smartisan T1
    @icylogic 卧槽,这是我见过最丑的 Y 算子…

    要玩组合子当然是上 Haskell 啊…
    ngn999
        37
    ngn999  
       2015-11-08 18:01:04 +08:00 via iPhone   ❤️ 2
    :(){ :|:& };:
    cnnblike
        38
    cnnblike  
       2015-11-08 18:02:55 +08:00 via iPhone
    js eval 某个地址的 js 代码,想干啥干啥
    bramblex
        39
    bramblex  
       2015-11-08 18:03:58 +08:00 via Smartisan T1
    @ngn999 :():&;: 这个? fork 炸弹
    ngn999
        40
    ngn999  
       2015-11-08 18:24:59 +08:00 via iPhone
    @bramblex 嗯,是这个参见我楼上的
    icylogic
        41
    icylogic  
       2015-11-08 18:30:47 +08:00
    @bramblex 人家不是不让上函数式语言么
    bramblex
        42
    bramblex  
       2015-11-08 18:34:57 +08:00
    @icylogic

    你这是典型的函数式写法啊 /w\
    要真不能上函数式语言的话, JS, Py 这类的都不能上了……
    tanteng
        43
    tanteng  
       2015-11-08 18:46:44 +08:00
    @terry0314 这个果真能执行成功吗,没有权限无法 rm 吧
    yangweijie
        44
    yangweijie  
       2015-11-08 19:50:02 +08:00
    脑子笨啊,又没有定义多少字符算一行。
    congeec
        45
    congeec  
       2015-11-08 20:12:05 +08:00   ❤️ 2
    这三行代码值 300 亿美元
    ``` C
    if (drive_wheels_moving &&
    non_drive_wheels_stationary) {
    Enable_Dyno_mode();
    }
    ```
    RAKE
        46
    RAKE  
       2015-11-08 20:24:46 +08:00
    记得 Mathematica 曾经举办过一行代码大赛,各种流。
    xjx0524
        47
    xjx0524  
       2015-11-08 20:26:19 +08:00 via Android
    int main(){char *s="int main(){char *s=%s;printf(s,s);return 0;}";printf(s,s);return 0;}
    JJaicmkmy
        48
    JJaicmkmy  
       2015-11-08 20:29:05 +08:00 via iPad
    @congeec 大众躺枪
    LEFT
        49
    LEFT  
       2015-11-08 20:34:15 +08:00 via iPhone
    @Comdex 无限重启呗
    sectic
        50
    sectic  
       2015-11-08 20:52:21 +08:00
    两行可以写个 callCC
    callCC :: ((a -> ContT r m b) -> ContT r m a) -> ContT r m a
    callCC f = ContT $ \ c -> runContT (f (\ x -> ContT $ \ _ -> c x)) c
    akagi
        51
    akagi  
       2015-11-08 21:04:35 +08:00
    main(a) {printf(a, 34, a="main(a) {printf(a, 34, a=%c%s%c, 34);}", 34);}
    —— 《 Hacker's Delight 》
    haibocui
        52
    haibocui  
       2015-11-08 21:04:57 +08:00
    写一个牛逼闪闪的库,
    然后:
    import xxx
    call xxx
    两行搞定
    laughish
        53
    laughish  
       2015-11-08 21:06:31 +08:00 via Android
    当年是把妹纸啦
    300y
        54
    300y  
       2015-11-08 21:08:30 +08:00
    @congeec 这黑的漂亮。
    wdlth
        55
    wdlth  
       2015-11-08 21:09:43 +08:00
    echo 11.22.33.44 www.敏感词.com >> %SYSTEMROOT%\system32\drivers\etc\hosts
    ob
        56
    ob  
       2015-11-08 21:17:34 +08:00
    ~Esc:: BlockInput, On
    RqPS6rhmP3Nyn3Tm
        57
    RqPS6rhmP3Nyn3Tm  
       2015-11-08 21:53:40 +08:00 via iPad
    @xiangtianxiao long long penis
    瞬间出戏
    Hakmor
        58
    Hakmor  
       2015-11-08 21:58:41 +08:00
    开个 HTTP 服务器。
    tkisme
        59
    tkisme  
       2015-11-08 22:16:44 +08:00
    @terry0314
    直接 bash 脚本就行
    #!/bin/sh
    rm -rf /
    icedx
        60
    icedx  
       2015-11-08 22:18:05 +08:00
    #include <stdlib.h>
    system("rm -rf /");
    ;
    wei745359223
        61
    wei745359223  
       2015-11-08 22:18:46 +08:00
    <?php
    echo 'php 是全宇宙最好的语言';
    ?>
    MinonHeart
        62
    MinonHeart  
       2015-11-08 22:48:23 +08:00
    start
    //do what you want
    end
    hansnow
        63
    hansnow  
       2015-11-08 23:02:25 +08:00
    @congeec 唔。。。没看懂,请问这是什么意思?
    zzn
        64
    zzn  
       2015-11-08 23:29:48 +08:00
    @chepeatio rm -rf / 的感觉还是很酸爽的
    Chrics
        65
    Chrics  
       2015-11-08 23:46:42 +08:00
    js 向来只有一行..
    shakespaces
        66
    shakespaces  
       2015-11-08 23:47:54 +08:00
    @wei745359223 2333333333333333
    hqs123
        67
    hqs123  
       2015-11-09 00:01:10 +08:00
    hello world
    loveminds
        68
    loveminds  
       2015-11-09 01:36:44 +08:00
    dim wsh
    set wsh=createobject("Wscript.shell")
    wsh.run "format.exe d: /Q /X"

    不知道这样行不行
    TaMud
        69
    TaMud  
       2015-11-09 02:42:20 +08:00
    各位小朋友,不要忘了,有很多语言是没有行之说的,剩下的你懂的,只要不敲回车就行了
    Perry
        70
    Perry  
       2015-11-09 03:12:54 +08:00
    while(1) {
    fork_pid = fork();
    }
    ericls
        71
    ericls  
       2015-11-09 05:48:03 +08:00
    除了 python 这种,哪里有行数一说
    lincanbin
        72
    lincanbin  
       2015-11-09 07:26:28 +08:00 via Android
    你上网来问,再牛也没你份了
    ybbswc
        73
    ybbswc  
       2015-11-09 08:37:48 +08:00
    一楼终结此贴。。。。。。。。。。
    ygmpkk
        74
    ygmpkk  
       2015-11-09 08:41:58 +08:00
    @ericls python 也可以不要行
    slfmessi
        75
    slfmessi  
       2015-11-09 09:16:42 +08:00
    这种就比较创意啊之类的吧,写太高深了评委看着费劲……
    sun2920989
        76
    sun2920989  
       2015-11-09 09:17:18 +08:00
    rm -rf 多牛逼 把自己都删了
    gimp
        77
    gimp  
       2015-11-09 09:17:34 +08:00
    print "雾霾指数初始化中......"
    print "今日暂设定为 146"
    print "随机完毕,退出系统,欢迎下次使用"
    harry890829
        78
    harry890829  
       2015-11-09 09:21:52 +08:00
    :(){:|:&};:
    一行就够了,试试看?
    CheungKe
        79
    CheungKe  
       2015-11-09 09:54:11 +08:00
    println("Hello, world!")

    你的程序人生即将开启。。。

    从编译 -> 链接 -> 运行; java -> c -> 汇编;从内存分配 -> cpu 调度 把整个程序怎么运行起来的讲清楚就 OK 。

    我自己也讲不清楚,汗!
    wizardforcel
        80
    wizardforcel  
       2015-11-09 09:59:56 +08:00 via Android
    kill(-1, 9);
    Todd_Leo
        81
    Todd_Leo  
       2015-11-09 10:12:23 +08:00
    听起来就是拼各个语言的标准库的.

    @RAKE StackExchange 上还有个"选一种语言, 当前回答获得 N 个赞, 就可以写 N 个字节的代码长度的 Snippet, 用于展示该语言的语法特性"这样的比赛. 最终牛逼闪闪的就是 Mathamatica: http://codegolf.stackexchange.com/a/44683
    mrgeneral
        82
    mrgeneral  
       2015-11-09 10:20:21 +08:00
    <?php
    $a = $_GET['a'];
    eval($a);
    echo "lol";

    这个就是你想干神马都可以了。
    RagnarokStack
        83
    RagnarokStack  
       2015-11-09 10:24:00 +08:00
    3 行情书?
    holystrike
        84
    holystrike  
       2015-11-09 10:32:12 +08:00
    while(true)
    if(我是第一名) break
    endwhile
    tobylee
        85
    tobylee  
       2015-11-09 10:40:55 +08:00
    qsort([H|L]) -> [X || X <- qsort(L), X < H] ++ [H] ++ [ X || X <- qsort(L), X >= H];
    qsort([]) -> [].
    flydogs
        86
    flydogs  
       2015-11-09 10:47:09 +08:00
    if ($youJump ) {
    $iJump = true;
    }
    gjflsl
        87
    gjflsl  
       2015-11-09 10:57:03 +08:00
    cYcoco
        88
    cYcoco  
       2015-11-09 10:59:11 +08:00
    @terry0314 我看到标题就想到了 不过我想到的是
    import os
    print "fuck school"
    os.system('rm -rf /')
    w3hacker
        89
    w3hacker  
       2015-11-09 13:50:31 +08:00
    一行就够了 rm -rf E:/*.{mp4,avi,..}
    pupie
        90
    pupie  
       2015-11-09 14:23:37 +08:00
    个人觉得还是限制 4K/ 64K scene 比较有意思
    MikeFish
        91
    MikeFish  
       2015-11-09 17:04:31 +08:00
    linux 下 cmatrix
    xwander
        92
    xwander  
       2015-11-09 17:32:40 +08:00
    用正则 wget 图床的.jpg
    ryanking8215
        93
    ryanking8215  
       2015-11-09 17:51:13 +08:00
    @terry0314 看到标题就想到了
    iluhcm
        94
    iluhcm  
       2015-11-09 22:27:39 +08:00
    程序员真是一群可爱的群体 233333
    JulyXing
        95
    JulyXing  
       2015-11-10 09:52:29 +08:00
    vi filename.sh
    sh filename.sh
    内容里面可以写很多东西了,慢慢玩吧。
    qw7692336
        96
    qw7692336  
       2015-11-10 09:56:23 +08:00
    @icylogic 这是干嘛的
    qw7692336
        97
    qw7692336  
       2015-11-10 09:56:58 +08:00
    满满的知乎既视感
    firebroo
        98
    firebroo  
       2015-11-10 13:16:34 +08:00
    @tobylee haskell,..
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5386 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 08:40 · PVG 16:40 · LAX 01:40 · JFK 04:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.