兔子问题----算法基础
2016-12-21 09:41
218 查看
每日一练,坚持就是胜利。
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
程序分析:第一个月一对兔子,第二个月一对兔子,第三个月两对兔子,第四个月三对兔子,第五个月五对兔子…………由此可以推出一个数字序列。1、1、2、3、5、8、13、21……由此可以发现这些数字的规律:前两个数之和等于第三个数,这样程序就出来了
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
程序分析:第一个月一对兔子,第二个月一对兔子,第三个月两对兔子,第四个月三对兔子,第五个月五对兔子…………由此可以推出一个数字序列。1、1、2、3、5、8、13、21……由此可以发现这些数字的规律:前两个数之和等于第三个数,这样程序就出来了
/** * 递归的算法 * @param mounth * @return */ public static int address(int mounth) { if (mounth == 1 || mounth ==2) { return 1; }else { return address(mounth - 1) + address(mounth - 2); } }
/** * 循环的算法 * @param mounth * @return */ public static int reserve(int mounth) { int[] num = new int[mounth+1]; num[0] = 0; num[1] = 1; int index = 2; if (mounth>1) { while (index < mounth+1) { num[index] = num[index - 1] + num[index - 2]; index++; } } return num[mounth]; }
相关文章推荐
- 基础算法题----兔子问题(斐波那契数列)
- 狐狸吃兔子问题算法
- 基础算法系列总结:动态规划(解公司外包成本问题)
- 算法基础——1.6练习(数字数码管、隐藏密码问题)
- 算法基础-动态规划 (1) 01背包问题
- 算法问题求解基础
- 算法习作:兔子生兔子问题 (-_-!)
- 算法实例一 算法问题求解基础--欧几里得递归算法和递归算法
- 算法实验题 车皮编序问题(转化为在全排列的基础上的堆栈问题)
- 算法基础经典问题
- 基础图论问题算法总结
- 基础算法系列总结:回溯算法(解火力网问题)
- 算法--兔子生兔子的问题(费氏数列)
- 算法--兔子生兔子的问题(费氏数列)
- 算法基础——1.1暴力破解法(鸡兔同笼、韩信点兵问题)
- 经典的100个C算法__(1)兔子问题
- 基础算法系列总结:回溯算法(解火力网问题)
- 哈工大 基础算法 1001 计数问题
- 兔子算法问题
- 基础算法之最大子列求和问题