求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
//利用构造函数求解/*class Temp{public: Temp(){N++;SUM+=N;}static void Reset(){N=0;SUM=0;}static int SumOf1ToN(){return SUM; }private:static int N; //声明static int SUM;};int Temp::N=0; //定义int Temp::SUM=0;class Solution {public:int Sum_Solution(int n) {Temp::Reset();Temp *a=new Temp[n]; //构造函数的作用,初始化形象的数据成员。delete[]a;a=NULL;return Temp::SumOf1ToN();}};*///利用&&短路原则class Solution {public:int Sum_Solution(int n) {int sum=n;sum>0&&(sum=sum+Sum_Solution(n-1));return sum;}};