Wins the offer (47)

Title: seeking 1 + 2 + 3 + ... + n, requires multiplication and division can not be used, for, while, if, else, switch, case and keywords such as conditional statement (A B:? C).

Ideas:
1. use of logic required to achieve short-circuit characteristic and the termination of recursion.
2. When n == 0 time, (n> 0) && ( (sum + = Sum_Solution (n-1))> 0) is performed only in front of the judgment is false, and then directly returns 0;
3. When n> 0 performing sum + = Sum_Solution (n-1 ), to achieve the recursive computation Sum_Solution (n)

public class Solution {
	public static void main(String[] args) {
	Solution solution = new Solution();
	System.err.println(solution.Sum_Solution(10));

	}
	public int Sum_Solution(int n) {
		int sum = n;
		boolean ans = (n>0)&&((sum+=Sum_Solution(n-1))>0);
		return sum;
	}
}
Published 49 original articles · won praise 0 · Views 339

Guess you like

Origin blog.csdn.net/weixin_46108108/article/details/104216375