2019-09-10[剑指offer-]求1+2+3+...+n

2019-11-15  本文已影响0人  Coding破耳

题目描述

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

class Solution {
public:
    
    typedef int (*fun)(int);
    int static sum_func1(int n) {return 0;}
    int static sum_func2(int n) {
        static fun f[2] = {sum_func1, sum_func2};
        return n + f[!!n](n-1);
    }
    int Sum_Solution(int n) {
        if(n <= 0)
            return 0;
        return sum_func2(n);
    }
};
上一篇 下一篇

猜你喜欢

热点阅读