/** * @author Eightn0 * @create 2021-03-17 20:09 * 说明:第二年、第四年下崽,第五年死去,第15年有几只羊? * 分析:这是一个繁殖问题,可以用递归的思想解决 */ //定义一个羊,有生和死两种方法 public class SheepCount { public int Sheep(int year){ if (year < 0){ return 0; } else if (year == 0 || year == 1){ return 1; } else return (year > 5 ? 0 : 1) + Sheep(year -2) + Sheep(year - 4); } public static void main(String[] args) { SheepCount sheepCount = new SheepCount(); System.out.println(sheepCount.Sheep(15)); } }
繁殖问题——母羊生小母羊
最新推荐文章于 2022-08-14 22:06:42 发布