|
1+..+n=n*(n+1)/2=(n^2+n)/2it is easy to get x/2, so the problem is to get n^2though no if/else is allowed, we can easilly go around using short-pass.using macro to make it fancier:#define T(X, Y, i) (Y & (1<<i)) && X+=(Y<<i)intfoo(int n){ int r=n; T(r, n, 0); T(r, n,1); T(r, n, 2); … T(r, n, 31); return r >> 1;} |
|
共 1 个关于本帖的回复 最后回复于 2013-8-16 11:09