Title Description Description
Given n (n>=1), A recursive method is used to calculate 1+2+3+4+...+ (n-1) +n
input/output format input/output
Input Format:
A row, a number n
output Format:row, the result
input and Output sample sample Input/output
sample Test point # #
Input Sample:3
Sample output:
6
Ideas:
Run the program, when T=5, the output: s=15, its recursive call execution process is: (set t=3)
recursive call procedure, is essentially the process of constantly calling a procedure or function, due to recursive invocation, all the subroutine variables (local variables, variable parameters, etc.), the address within the computer has a special management method-Stack (advanced post-out) to manage, once the recursive call ends, The computer begins to return the values of each subroutine variable based on the address stored in the stack and operates accordingly.
the code ① is as follows (conforms to test instructions, with recursion):
1#include <stdio.h>2 intFacintN)3 {4 if(n==0)return 0;5 Else 6 {7 return(FAC (n1) +n);//Recursive8 }9 }Ten intMain () One { A intN; -scanf"%d",&n); -printf"%d\n", FAC (n));//calling Functions the return 0; -}
The code ② as follows (self-written, useless recursion):
1#include <stdio.h>2 intMain ()3 {4 intN;5scanf"%d",&n);6printf"%d\n",((1+N) *n)/2);7 return 0;8}
Arithmetic progression problems