题目

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

    $1 ≤ n ≤ 1000$。

    示例

    输入:10

    输出:55

    代码

    • 短路与
    class Solution {
    public:
        int getSum(int n) {
            int res = n;
            n > 0 && (res += getSum(n-1));
            return res;
        }
    };
    
    • 等差求和
    class Solution {
    public:
        int getSum(int n) {
            char a[n][n+1];
            return sizeof(a)>>1;
        }
    };
    

    本文题目求1+2+…+n

    最后修改:2022 年 02 月 08 日 06 : 04 PM
    如果我的文章对你有用,请随意赞赏