在 Hacker News 上看到这个讨论串
https://news.ycombinator.com/item?id=7828757
题目很简单: 写个程式“看起来”是在计算 2+2 但最后输出 "2+2" 的答案为 5
"2 + 2 = 5" 这个梗来自于 George Orwell 所著 1984 这本小说
以下程式码节选自网友参赛作品
============================================================
By http://codegolf.stackexchange.com/users/17249/durron597
============================================================
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);
}
}
============================================================
============================================================
By http://codegolf.stackexchange.com/users/17249/durron597
============================================================
public class TwoPlusTwo {
public static void main(String... args) {
double two = two();
System.out.format("Variable two = %.15f%n", two);
double four = Math.ceil(two + two); // round just in case
System.out.format("two + two = %.15f%n", four);
}
// 20 * .1 = 2
private static double two() {
double two = 0;
for(int i = 0; i < 20; i++) {
two += .1;
}
return two;
}
}
============================================================
============================================================
Originally written in C# by http://codegolf.stackexchange.com/users/20329/grax
============================================================
public class HelloWorld{
public static void main(String []args){
int x = 2;
int y = 2;
if (false);
{
++x;
}
System.out.println(x+y);
}
}
============================================================
============================================================
By http://codegolf.stackexchange.com/users/7416/aditsu
============================================================
public class Five {
public static void main(final String... args) {
System.out.println(256.0000000000002 + 256.0000000000002);
}
}
============================================================
同场加映,猜猜看以下这个程式的 output
============================================================
By https://news.ycombinator.com/user?id=kyrra
============================================================
public class A {
public static void main(String[] args) {
Integer a = 100, b = 100;
Integer c = 10000, d = 10000;
System.out.println(a == b);
System.out.println(c == d);
}
}
============================================================
该把 "Can understand Java code" 这行从我的履历表上拿掉了… :D