Fork me on GitHub

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

题目描述

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

public class Main46 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(Main46.Sum_Solution(4));
	}
	
	public static int Sum_Solution(int n) {
		 int res = n; 
	     boolean flag = (n>0)&&((res+=Sum_Solution(n-1))>0); 
	     return res; 
    }

}

  

posted @ 2019-07-15 14:44  gentleKay  阅读(136)  评论(0编辑  收藏  举报