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

题目描述

求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
package new_offer;
/**
 * 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case
 * 等关键字及条件判断语句(A?B:C)。
 * @author Sonya
 *思路:使用递归利用 逻辑与短路特性,&& 如果前面不满足则不执行后面的。
 */
public class N47_Sum1_N {
	 public int Sum_Solution(int n) {
	       int sum=n;
	       boolean ans=(n>0)&&((sum+=Sum_Solution(n-1))>0);
	       return sum;
	    }

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

  

posted @ 2019-07-12 14:26  柯汐  阅读(191)  评论(0编辑  收藏  举报