一、题目
求1+2+3+...+n_牛客题霸_牛客网
二、代码
1.使用静态成员变量+构造函数
class SUM
{private:static int _i;static int _ret;public:SUM(){_ret = _ret + _i;_i++;}static int GetRet(){return _ret;}
};
int SUM::_i=1;
int SUM::_ret=0;class Solution {
public:int Sum_Solution(int n) {SUM s[n];return SUM::GetRet();}
};
2.使用内部类
class Solution {class SUM {public:SUM() {_ret = _ret + _i;_i++;}static int GetRet() {return _ret;}};public:int Sum_Solution(int n) {SUM s[n];return SUM::GetRet();}private:static int _i;static int _ret;
};//类外定义
int Solution::_i = 1;
int Solution::_ret = 0;