题解 | #统计每个月兔子的总数#

https://www.nowcoder.com/practice/1221ec77125d4370833fd3ad5ba72395

function countRabbit(str = '') {
  let monthe = parseInt(str);
  if (monthe < 3) return 1;
  let one = 0;
  let two = 0;
  let other = 1;
  for(let i = 3; i <= monthe; i++) {
    // 算出 每个月, 三种兔子的数量
    other += two; // 这个月 两个月大的兔子会生一个
    two = one; // 这个月 上个月一月大的兔子变成两个月大
    one = other; // 这个月 会生出一个月的兔子
  }
  return one + two + other;
}

console.log(countRabbit('3'))
全部评论

相关推荐

头像
不愿透露姓名的神秘牛友
04-29 12:10
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务