题解 | #求1+2+3+...+n#

求1+2+3+...+n

https://www.nowcoder.com/practice/7a0da8fc483247ff8800059e12d7caf1

class Add
{
public:
    Add()
    {
        _ret += _i;   
        _i++;
    }
    
    static int GetRet()
    {
        return _ret;
    }
    
    static void Init()
    {
       _i = 1;
       _ret = 0;
    }
    
private:
    static int _i;
    static int _ret;
};
    
int Add::_i = 1;
int Add::_ret = 0;


class Solution {
public:
 
    int Sum_Solution(int n) {
        Add::Init();//这里编译器为了通过多个测试用例,要多次调用接口,需要及时把_i和_ret初始化
        Add arr[n];//新一点的编译器支持变长数组c99,即变量来定义数组
        
        return Add::GetRet();
    }
};
全部评论

相关推荐

05-09 13:22
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务