剑指offer--47.求1+2+3+...+n

题目描述

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

时间限制:1秒 空间限制:32768K 热度指数:137906

思路

见代码

class Solution {
public:
    int Sum_Solution(int n) {
        //逻辑与,逻辑与有个短路特点,前面为假,后面不计算。接着用递归计算
        int sum=n;
        n && (sum+=Sum_Solution(n-1));
        return sum;
    }
};

你可能感兴趣的:(剑指offer)