第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > 求1+2+3+...+n 要求不能使用乘除法 for while if else switch case等关键字及条件判断语句(A?B:C)。

求1+2+3+...+n 要求不能使用乘除法 for while if else switch case等关键字及条件判断语句(A?B:C)。

时间:2021-04-23 16:51:36

相关推荐

求1+2+3+...+n 要求不能使用乘除法 for while if else switch case等关键字及条件判断语句(A?B:C)。

求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;}};

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。