-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJz7.java
More file actions
82 lines (66 loc) · 1.49 KB
/
Jz7.java
File metadata and controls
82 lines (66 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.cpucode.java.getting.started;
/**
* 题目描述
* 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0,第1项是1)。
* n\leq 39n≤39
*
* 示例1
* 输入
* 4
* 返回值
* 3
*
* @author : cpucode
* @Date : 2021/1/19
* @Time : 9:46
* @Github : https://github.com/CPU-Code
* @CSDN : https://blog.csdn.net/qq_44226094
*/
public class Jz7 {
public static void main(String[] args) {
Jz7 test = new Jz7();
int num = 4;
int solu = 0;
solu = test.Fibonacci(num);
System.out.println(solu);
}
public int Fibonacci(int n) {
int num = 0;
num = test1(n);
//num = test2(n);
//num = test3(n);
return num;
}
private static int test1(int n){
if(n <= 1){
return n;
}
return test1(n - 1) + test1(n - 2);
}
private static int test2(int n){
if(n <= 1){
return n;
}
int sum = 0;
int one = 1;
int two = 0;
for(int i = 2; i <= n; i++){
sum = one + two;
two = one;
one = sum;
}
return sum;
}
private static int test3(int n){
if(n <= 1){
return n;
}
int one = 0;
int sum = 1;
for(int i = 2; i <= n; i++){
sum += one;
one = sum - one;
}
return sum;
}
}