A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 黑妞~ 金牌黑马   /  2014-7-3 09:32  /  2932 人查看  /  16 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

今天在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\n”, 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 = ‘\x312\x2D7′
>>> 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;
}

各位有什么补充的,发挥你的想象吧,评论中告诉我们。



16 个回复

正序浏览
C++那里还是看懂了一些
回复 使用道具 举报
第一个会挂了
回复 使用道具 举报
还是不懂
回复 使用道具 举报
看不懂啊
回复 使用道具 举报
收藏下!!!!
回复 使用道具 举报
哇,楼主你有点牛逼哦!
回复 使用道具 举报
菜鸟看不懂嘎。
回复 使用道具 举报
看是看的懂,但是之前却没见过
回复 使用道具 举报
自己设计个算法--
回复 使用道具 举报
好高端,,,
回复 使用道具 举报
张斌 中级黑马 2014-7-14 09:04:20
7#
只想说太有才了啊
回复 使用道具 举报
看不懂啊。。。
回复 使用道具 举报
这就是我向往的代码世界,丰富多彩,富有趣味性!
回复 使用道具 举报
本帖最后由 OCTSJimmy 于 2014-7-6 13:48 编辑

JAVA看了下,没看很懂,刚开始学来着,应该用的是反射,可为什么要反射修改CACHE就完全不理解了,另外array里面到底是什么也没深究……
Javascript,算是我比较熟悉的代码之一了,试了下,我晕死,完全醉啦……
把g拿掉居然会报错{:3_46:}错误居然还是H未定义,这什么情况来着啊……{:3_46:}
在alert(f())前面插入alert(H)居然还会有值,而且值还是无比神奇的3……我真醉啦……
仔细检查一遍,真醉啦,原来这明明是俩H……一个是unicode中的H,有点类似全角与半角的概念,但,这个H也不是全角……我。。。醉晕啦……

然后JAVA的反射,实际上是对实例池动手脚,详细解释如下:

Java中的整型实例池的概念。以前只看到过实例池导致两个对象的指针相同的问题,即


Integer a = new Integer(2);
Integer b = new Integer(2);
System.out.print(a == b);


上面的代码最终输出的是true,按照Java对象的申请原则来说,这里应该是false才对。正是因为JVM在实现的时候,默认生成了一些 Integer对象的实例,当需要的实例是池子中已经存在的数值时,直接返回已经生成的对象的引用,不必新构造对象。这样可以极大减少实例数目和程序运行 性能。
而这个题目是将池子中的对象的内容进行了修改,最终使得取回的实例的值发生了改变。这样其实很危险的,如果在正式运行程序的业务代码之前,做这个修改,那么整个程序的运行逻辑将产生混乱。

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,作者给出的解释为:
You need to change it even deeper than you can typically access. Note that this is designed for Java 6 with no funky parameters passed in on the JVM that would otherwise change the IntegerCache.
Deep within the Integer class is a Flyweight of Integers. This is an array of Integers from −128 to +127. cache[132] is the spot where 4 would normally be. Set it to 5.
利用缓存的读写接口,将4这个实例的缓存对象的指针改为指向5的实例对象了,这样,当应用程序取出4时,实际上返回的是5的引用,打印出来的也就是5了。

回复 使用道具 举报
哈哈,不太懂
回复 使用道具 举报
菜鸟看不懂.....
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马