Seeking to prove safety Offer-- 1 + 2 .. + n, and

1, Title Description

  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).

2, code implementation

public int Sum_Solution(int n) {
        int sum = n;
        boolean ans = (n>=1)&&((sum+=Sum_Solution(n-1))>0);
        return sum;
    }

  

Guess you like

Origin www.cnblogs.com/BaoZiY/p/11183372.html