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

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

方法一

    public int Sum_Solution(int n) {
    	return (int)(Math.pow(n, 2) + n) >> 1;
    }

方法二

    public int Sum_Solution_2(int n) {
    	int sum = n;
    	boolean ans = (n > 0) && ((sum += Sum_Solution(n-1))>0);
    	return sum;
    }
posted @ 2019-04-03 21:31  如是说  阅读(148)  评论(0编辑  收藏  举报