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

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

思路:一般题目这样要求就是要用位运算。。这里利用短路的特点。n==0时,res&(Sum_Solution(n-1))只执行前面的,作为递归出口

int Sum_Solution(int n) {
        int res=n;
        res &&(res+=Sum_Solution(n-1));
        return res;
    }

 

posted @ 2017-04-11 02:04  雪浪snowWave  阅读(181)  评论(0编辑  收藏  举报