Wins the offer - 1 + 2 + 3 + ... + n multiplication and division are not available, determining, for, if

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

Thinking

For connecting the two parts and a first part = 0, the second portion of the operation will not be carried out, this is called short-circuit

Note that the following statement is a = n and (...) n only used for the determination, if n! = 0, then a value that depends on the number of the back.

class Solution:
    def Sum_Solution(self, n):
        ans = n
        ans = n and (ans + self.Sum_Solution(self,n-1))
        return ans

 

Published 82 original articles · won praise 2 · Views 4360

Guess you like

Origin blog.csdn.net/qq_22498427/article/details/104766155