• 各种编程语言实现 2 + 2 = 5


    今天在stackexchange上看到一篇非常有趣的帖子,是关于如何用各种编程语言实现 2 + 2 = 5 的,2 + 2怎么会等于5呢?我一开始也很不解,不过看了下面各种编程语言实现的方法,我震惊了,让我又一次相信人类真是一种不可思议的生物。

    1、JAVA

    import java.lang.reflect.Field;

    public class Main {
    public static void main(String[] args) throws Exception {
    Class cache = Integer.class.getDeclaredClasses()[0];
    Field c = cache.getDeclaredField(“cache”);
    c.setAccessible(true);
    Integer[] array = (Integer[]) c.get(cache);
    array[132] = array[133];

    System.out.printf(“%d”,2 + 2);
    }
    }

    输出:

    5

    2、C

    int main() {
    char __func_version__[] = “5″; // For source control
    char b[]=”2″, a=2;
    printf(“%d + %s = %s ”, a, b, a+b);
    return 0;
    }

    3、C (Linux, gcc 4.7.3)

    #include <stdio.h>

    int main(void)
    {
    int a=3, b=2;

    printf(“%d + %d = %d”, –a, b, a+b);
    }

    4、Haskell

    λ> let 2+2=5 in 2+2
    5

    5、BBC BASIC

    MODE 6
    VDU 23,52,254,192,252,6,6,198,124,0
    PRINT
    PRINT “2+2=”;2+2
    PRINT “2+3=”;2+3

    6、Python

    >>> patch = ‘x312x2D7′
    >>> import ctypes;ctypes.c_int8.from_address(id(len(patch))+8).value=eval(patch)
    >>> 2 + 2
    5

    7、JavaScript

    g = function () {
    H = 3
    return H + H
    }

    f = function () {
    Η = 2
    return Η + H
    }

    // 3 + 3 = 6
    alert(g())
    // 2 + 2 = 5
    alert(f())

    8、Bash

    v=2 #v is 2
    v+=2 #v is 4
    v=$(($v*5)) #v is 20
    v=$(($v-16)) #v is 4
    v=$(bc<<<”sqrt($v)+2″) #v is 4 (sqrt(4) is 2)
    v=$(bc<<<”$v/4+3″) #v is 4 (4/4 = 1)
    echo ’2+2=’ $v #So v is 4…?

    9、PHP

    echo ’2 + 2 = ‘ . (2 + 2 === 4 ? 4 : 2 + 2 === 5 ? 5 : ‘dunno’);

    10、Perl

    # Generic includes
    use strict;
    use warnings;
    use 5.010;
    use Acme::NewMath;

    # Ok, time to begin the real program.
    if (2 + 2 == 5) {
    say 5;
    }
    else {
    say “Dunno…”;
    }

    11、C#

    static void Main(string[] args)
    {
    var x = 2;
    var y = 2;

    if (1 == 0) ;
    {
    ++x;
    }

    Console.WriteLine(x + y);
    }

    12、C++

    #include <iostream>

    class Int
    {
    public:
    Int(const int& a) : integ(a) {}

    friend std::ostream& operator<<(std::ostream& oss, const Int& rhs)
    {
    return oss << rhs.integ;
    }
    int operator+(Int o)
    {
    if(integ == 2 && o.integ == 2)
    return integ+o.integ+1;
    return integ+o.integ;
    }

    private:
    int integ;
    };

    int main()
    {
    Int two = 2;
    std::cout << two << ” + ” << two << ” = ” << two + two;
    }

  • 相关阅读:
    centos7搭建ELK开源实时日志分析系统
    基于ELK的简单数据分析
    用ELK打造可视化集中式日志
    elk单台环境搭建
    用logstash,elasticSearch,kibana实现数据收集和统计分析工作
    用Kibana和logstash快速搭建实时日志查询、收集与分析系统
    elasticsearch按照配置时遇到的一些坑 [Failed to load settings from [elasticsearch.yml]]
    分布式搜索elasticsearch几个概念解析
    分布式搜索elasticsearch配置文件详解
    CENTOS安装ElasticSearch
  • 原文地址:https://www.cnblogs.com/zaiiiPan/p/4502581.html
Copyright © 2020-2023  润新知