首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

JAVA经典算法首先题

2012-12-20 
JAVA经典算法第一题【程序1】题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第

JAVA经典算法第一题
【程序1】   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?  
1.程序分析:   兔子的规律为数列1,1,2,3,5,8,13,21....  

public class rabbit{public static void main(String args[]){int i=0;for(i=1;i<=20;i++)System.out.println(f(i));}public static int birth(int x){if(x==1 || x==2)return 1;elsereturn f(x-1)+f(x-2);}}或public class rabbit{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 birth (int x){if(x==1 || x==2)return 1;elsereturn f(x-1)+f(x-2);}}

热点排行