美文网首页
面试题64:求1+2+....+n

面试题64:求1+2+....+n

作者: 繁星追逐 | 来源:发表于2019-11-22 11:31 被阅读0次

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

  • 要求不能使用乘除法、for、while、if、else、switch、case等关键词以及三元运算符等。
/**
     * 1+2+3..+n  = n*(n+1)/2 = n2/2 + n/2
     * @param n
     * @return
     */
    public int Sum_Solution1(int n) {
       if (n < 1) return 0;
       return (int)(Math.pow(n,2) + n) >> 1;
    }

    /**
     * 解题思路:
     * 1.需利用逻辑与的短路特性实现递归终止。 2.当n==0时,(n>0)&&((sum+=Sum_Solution(n-1))>0)只执行前面的判断,为false,然后直接返回0;
     * 3.当n>0时,执行sum+=Sum_Solution(n-1),实现递归计算Sum_Solution(n)。
     * @param n
     * @return
     */
    public int Sum_Solution(int n) {
        int res = n;

        boolean b = (n > 0) && ((res += Sum_Solution(n-1)) > 0);
        return res;
    }

相关文章

网友评论

      本文标题:面试题64:求1+2+....+n

      本文链接:https://www.haomeiwen.com/subject/txbwbctx.html