Table of contents
3. Print each digit of the number sequentially
4. Find the sum of each digit of the number
5. Find the Fibonacci sequence
1. Find the factorial of N
(1) Analyze the meaning of the question
- For example, if you find the factorial of 5, the symbolic representation is 5! ;So 5! =5*4*3*2*1
- Let’s use simple recursion to complete this question. Let’s look at the recursive code.
public static int sub(int n) {
if(n==1) {
return 1;
}
return n * sub(n-1);
}
- The meaning of this code is analyzed below:
(2) Recursive thinking
- Disassembly algorithm: 6! =6*5! ;5! =5*4! ;4! =4*3! ;3! =3*2! ;2! =2*1! ;In fact, it is 6! =6*5*4*3*2*1.
- Expressed in code: The whole thing may be a bit messy, and readers need to calm down to understand it.
(3) Complete code
public static void main3(String[] args) {
//递归求N的阶乘
int N = 6;
int sum = sub(N);
System.out.println(sum);
}
public static int sub(int n) {
if(n==1) {
return 1;
}
return n * sub(n-1);
}
2. Find the sum of 1+2+...+N
(1) Analyze the meaning of the question
- Assume N=3, which means to find the sum from 1 to N (1+2+3)
- Assuming N=4, you need to find the sum of (1+2+3+4)
(2) Recursive thinking
- Here we take N=3 as an example
- Idea: Finding (1+2+3) can be decomposed into: 3+ (finding the sum of N=2), finding the sum of N=2 can be decomposed into: 2+ (finding the sum of N=1)
- resulting in recursive code
public static int sum(int n) {
if(n==1) {
return 1;
}
return n+sum(n-1);
}
- Let’s dissect the recursive idea:
(3) Complete code
public static void main(String[] args) {
//递归求和
int N = 3;
int sum = sum(N);
System.out.println(sum);
}
public static int sum(int n) {
if(n==1) {
return 1;
}
return n+sum(n-1);
}
3. Print each digit of the number sequentially
(1) Analyze the meaning of the question
- For example, if you print 1234, the result of printing the meaning of the question is 1 2 3 4 (spaced in the middle)
(2) Analyze recursive ideas
- Assume that the input data is 1234 and print out 1 2 3 4
- Idea: To print 1 2 3 4, print 1 first and then 2 3 4; to print 2 3 4, print 2 first and then 3 4; to print 3 4, print 3 first and then print 4.
- Use /10 to remove the lowest bit and %10 to get the lowest bit.
- resulting in recursive code
public static void print(int n) {
if(n<10) {
System.out.print(n+" ");
return;
}
print(n/10);
System.out.print(n%10+" ");
}
- Analyze recursive ideas
(3) Complete code
public static void main(String[] args) {
//顺序打印数组的每一位
int num = 1234;
print(num);
}
public static void print(int n) {
if(n<10) {
System.out.print(n+" ");
return;
}
print(n/10);
System.out.print(n%10+" ");
}
4. Find the sum of each digit of the number
(1) Analyze the topic
- For example, given the number: 1345, you need to find the sum of 1+3+4+5
(2) Analyze recursive ideas
- Here we are trying to find the sum of each digit of 1234
- Also use /10 to remove the lowest bit, and %10 to get the lowest bit.
- Idea: To find the sum of each digit of 1234, you can find the sum of each digit of 4+123; to find the sum of each digit of 123, you can find the sum of each digit of 3+12; to find the sum of each digit of 12 The sum of , you can find the sum of each digit of 1+2
- recursive code
public static int sumEvery(int n) {
if(n==1) {
return n;
}
return n%10+sumEvery(n/10);
}
- Recursive process analysis
(3) Complete code
public static void main(String[] args) {
//求数字的每一位之和
int N = 1234;
int sum = sumEvery(N);
System.out.println(sum);
}
public static int sumEvery(int n) {
if(n==1) {
return n;
}
return n%10+sumEvery(n/10);
}
5. Find the Fibonacci sequence
(1) Understand the Fibonacci sequence
- The values of the Fibonacci sequence are: 1, 1, 2, 3, 5, 8, 13, 21, 34... This sequence starts from the 3rd item, and each item is equal to the sum of the previous two items.
(2) Solving with recursive ideas
- For example, find the fifth Fibonacci number (5): 5=3 (fourth number) + 2 (third number); 3 = 2 (third number) + 1 (second number); And so on until n<=2.
- recursive code
public static int fib(int n) {
if(n<=2) {
return 1;
}
return fib(n-2)+fib(n-1);
}
- Analysis of recursive ideas
- Recursive complete code
public static void main(String[] args) {
//斐波拉契
int N = 8;
int sum = fib(N);
System.out.println(sum);
}
public static int fib(int n) {
if(n<=2) {
return 1;
}
return fib(n-2)+fib(n-1);
}
(3) Iterative ideas
- This idea is to use loops to calculate larger Fibonacci numbers.
- Utilize: a certain number = the sum of the previous two numbers, start from the front and count backward, and keep looping
- Code display:
public static void main(String[] args) {
//迭代思路
int N = 5;
int a = 1;
int b = 1;
int c = 1;
while(N>2) {
c = a+b;
a = b;
b = c;
N--;
}
System.out.println(c);
}
- Analysis of iteration ideas:
This time the five recursion questions are over.