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

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

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

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param n int整型 
 * @return int整型
 */
int Sum_Solution(int n ) {
    // write code here
    static int sum = 0;     //存储总和
    sum += n;
    n && (Sum_Solution(n-1));   //使用&&来结束递归,如果n=0则不再调用递归,不断返回到主函数
    return sum;
}

全部评论

相关推荐

见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务