【Questions】Seeking 1+2+3+...n

Find 1+2+3+...+n

JZ64 Find 1+n

Description
To calculate 1+2+3+...+n, it is required that keywords such as multiplication and division, for, while, if, else, switch, and case, and conditional judgment statements (A?B:C) cannot be used.

my train of thought

Circulation and multiplication and division are not allowed, only recursion + addition can be considered for calculation, and if is not allowed for the recursive termination condition, bit operations can be used.

class Solution {
    
    
public:
    int Sum_Solution(int n) {
    
      
        n && (n += Sum_Solution(n-1));
        return n;
    }
};

very clever operation

Guess you like

Origin blog.csdn.net/m0_73209194/article/details/131703899