編程真難。。。

上週,在Sun的Java論壇上出現了一個這樣的帖子,這個貼子的鏈接如下:
http://forums.sun.com/thread.jspa?threadID=5404590&start=0&tstart=0

LZ的貼子翻譯如下:

大家好,我是一個Java的新手,我有一個簡單的問題:請問我怎麼才能反轉一個整數的符號啊。比如把-12轉成+12。是的,毫無疑問這是個簡單的問題,但我弄了一整天我也找不到什麼好的方法。非常感謝如果你能告訴我Java有什麼方法可以做到這個事,或者告訴我一個正確的方向——比如使用一些數學庫或是二進制方法什麼的。謝謝!

這個貼子的沙發給出了答案:

n = -n;

LZ在四樓回覆到:

我知道是個很簡單的事,可我沒有想到居然這麼簡單,我覺得你可能是對的。謝謝你。

過了一會,又回覆到:

不開玩笑地說,我試了,真的沒有問題耶!

看到這樣的貼子,就能想到國內論壇上很多這樣的“問弱智問題的貼子”,結果可能都會是比較慘!是的,國外的互聯網文化和國內差不多,都是惡搞的人多於熱心的人,呵呵。不過,國外的網民們有一點是好的,再惡搞也是就事搞事,不會有侮辱人的語言,這點真是值國內的人學習

這本是一個平淡無奇的貼子,不過回覆中那些惡搞的“解決方案”太強大了,在這裏例舉一下吧。

貼子的板凳給出了這樣的答案(這是惡搞的開始)

 

1
2
3
4
5
6
7
8
9
10
int x = numberToInvertSign;
boolean pos = x > 0;
for(int i = 0; i < 2*Math.abs(x); i++){
    if(pos){
        numberToInvertSign--;
    }
    else{
        numberToInvertSign++;
    }
}

然後,有人說,n = -n 可以是可以,但不夠晦澀,於是一個晦澀的解決方案出現了:

1
2
int n = ....;
 n = (0xffffffff ^ n) + 1;

然後,又出現了一些看似簡單,其實是比較晦澀的方案

1
n = ~n + 1;

 

1
n = ~--n;

 

繼續,有才的人從來就不少:

1
2
3
4
5
6
n^= 0xffffffff;
int m;
for (m= 1; m != 0 && ((n&m) != 0); m<<= 1);
n|= m;
if (m == 0) n= m;
else for (m >>= 1; m != 0; n^= m, m>>=1);

 

呵呵,開始越來越強大了,我以前也向大家介紹過《如何加密/弄亂C源代碼》的文章,和這些惡搞的人可能有點相似吧。上面這個例子一出,大家都在討論上面例子中的for循環語句,呵呵,很費解啊。

然後,後面幾個就開始亂來了:

1
2
3
public int invert(int i) {
  return i - (i + i);
}
1
2
3
4
5
6
7
switch (i)
{
  case 1: return -1;
  case 2: return -2;
  case 3: return -3;
  // ... etc, you get the proper pattern
}

不過事情還沒有結束,看看下面這個吧,OMG。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int absoluteValue(int num)
{
 int max = 0;
 for(int i = 0; true; ++i)
 {
  max = i > max ? i : max;
  if(i == num)
  {
   if(i >= max)
    return i;
   return -i;
  }
 }
}

 還有用字符串的解決方案:

1
2
3
4
5
6
7
8
9
10
11
public int invert(int n) {
    String nStr = String.valueOf(n);
   
    if (nStr.startsWith("-")) {
        nStr = nStr.replace("-", "");
    } else {
        nStr = "-" + nStr;
    }
   
    return Integer.parseInt(nStr);
}

別忘了面象對象,有最新Java支持的模板庫: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
public interface Negatable<T extends Number> {
  T value();
  T negate();
}
   
   
   
public abstract class NegatableInteger implements Negatable<Integer> {
  private final int value;
   
  protected NegatableInteger(int value) {
    this.value = value;
  }
   
  public static NegatableInteger createNegatableInteger(int value) {
    if (value > 0) {
      return new NegatablePositiveInteger(value);
    }
    else if (value == Integer.MIN_VALUE) {
      throw new IllegalArgumentException("cannot negate " + value);
    }
    else if (value < 0) {
      return new NegatableNegativeInteger(value);
    }
    else {
      return new NegatableZeroInteger(value);
    }
  }
   
  public Integer value() {
    return value;
  }
   
  public Integer negate() {
    String negatedString = negateValueAsString ();
    Integer negatedInteger = Integer.parseInt(negatedString);
    return negatedInteger;
  }
   
  protected abstract String negateValueAsString ();
}
   
   
   
public class NegatablePositiveInteger extends NegatableInteger {
  public NegatablePositiveInteger(int value) {
    super(value);
  }
   
  protected String negateValueAsString () {
    String valueAsString = String.valueOf (value());
    return "-" + valueAsString;
  }
}
   
   
   
public class NegatableNegativeInteger extends NegatableInteger {
  public NegatableNegativeInteger (int value) {
    super(value);
  }
   
  protected String negateValueAsString () {
    String valueAsString = String.valueOf (value());
    return valueAsString.substring(1);
  }
}
   
   
   
public class NegatableZeroInteger extends NegatableInteger {
  public NegatableZeroInteger (int value) {
    super(value);
  }
   
  protected String negateValueAsString () {
    return String.valueOf (value());
  }
}

 

這個貼子基本上就是兩頁,好像不算太嚴重,如果你這樣想的話,你就大錯特錯了。這個貼子被人轉到了reddit.com,於是一發不可收拾,在上面的回貼達到了490多條。鏈接如下:

http://www.reddit.com/r/programming/comments/9egb6/programming_is_hard/

有人說,要用try catch;有人說要使用XML配置文件……,程序員們在追逐更爲變態和瘋狂的東西,並從中找到快樂,呵呵。

看完後,正如reddit.com所說——“編程好難啊”!

無獨有偶,這並不是第一次,也不會是最後一次,讓我們看看在PHP的官網上發生的類似的一幕——討論PHP的abs取絕對值函數的函數說明文檔中的回覆:

http://us.php.net/manual/en/function.abs.php#58508

又是一個長貼,還帶着很多性能分析,真的很好很強大!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章