不用if.else.while等一切的条件判断
这题真是涨知识了:
1.
#include<stdio.h> typedef int (*fun)(int); int f1(int i) { return 0; } int f2(int i) { fun f[2]={ f1,f2 }; return i+f[!!i](i-1); } int main() { int sum = f2(100); printf("%d ",sum); return 0; }
2. 运用c++的static性质
class Temp { public: Temp() { ++ N; Sum += N; } static void Reset() { N = 0; Sum = 0; } static int GetSum() { return Sum; } private: static int N; static int Sum; }; int Temp::N = 0; int Temp::Sum = 0; int solution1_Sum(int n) { Temp::Reset(); Temp *a = new Temp[n]; delete []a; a = 0; return Temp::GetSum(); }
3.
template <int n> struct solution4_Sum { enum Value { N = solution4_Sum<n - 1>::N + n}; }; template <> struct solution4_Sum<1> { enum Value { N = 1}; };
转自
http://zhedahht.blog.163.com/blog/static/2541117420072915131422/