Title: Ask 1+2+3+...+n, request cannot use multiplication method, for, while, if, else, switch, case and other keywords and conditional judgment statement (A? B:C).
This question is very interesting, the solution is still relatively difficult to think of, see this problem usually think of three more common methods: The first is also the most easy to think of, with the loop to do, but the topic requirements can not be used for, while; the second we'll think of is a arithmetic progression, with arithmetic progression's summation formula, But the problem is not to use the multiplier method, or not; the third we will gradually think of recursion, it is true that recursion can be done, but the recursive termination conditions need to use if, not as if.
The solution seems difficult to find, the following is a clever solution to the problem, the key is to use the operator && short-circuit characteristics as a recursive termination condition, which is equivalent to replacing if as a recursive termination condition.
1#include <cstdio>2 3 LongAddConst intNLong&sum) {4n && Add (N-1, sum);5 returnSum + =N;6 }7 8 intMain () {9 intN;Ten while(SCANF ("%d", &n)! =EOF) { One Longsum =0; Aprintf"%ld\n", add (n, sum)); - } - return 0; the}
[JOBDU] Topic 1506: Seeking 1+2+3+...+n