程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
程序设计:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class exp2{
public static void main(String args[]){
int i= 0 ;
for (i= 1 ;i<= 20 ;i++)
System.out.println(f(i));
}
public static int f( int x)
{
if (x== 1 || x== 2 )
return 1 ;
else
return f(x- 1 )+f(x- 2 );
}
}
|
或
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class exp2{
public static void main(String args[]){
int i= 0 ;
math mymath = new math();
for (i= 1 ;i<= 20 ;i++)
System.out.println(mymath.f(i));
}
}
class math
{
public int f( int x)
{
if (x== 1 || x== 2 )
return 1 ;
else
return f(x- 1 )+f(x- 2 );
}
}
|