`
yanzhihong23
  • 浏览: 59786 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Sheep Problem

阅读更多
// 1. sheep produce next generation on 2th and 4th year
// 2. sheep dies on 5th year
// 3. calculate total amount on certain year

var sheepfold = [],
    index = 0,
    amount;

function Sheep() {
  this.age = 1;
  this.index = index++;
}

Sheep.prototype.grow = function() {
  this.age += 1;

  if(/2|4/.test(this.age)) {
    sheepfold.push(new Sheep());
  } else if(this.age === 5) {
    for(var i = 0; i < sheepfold.length; i++) {
      if(this.index === sheepfold[i].index) {
        sheepfold.splice(i, 1);
        break;
      }
    }
  }
}

function calc(year) {
  if(!year || year < 1) return;

  for (var i = 1; i <= +year; i++) {
    if(i === 1) {
      sheepfold.push(new Sheep());
    } else {
      sheepfold.forEach(function(sheep) {
        sheep.grow();
      });
    }
  }

  return sheepfold.length;
}

amount = calc(20);

console.log('Total amount: ' + amount);

 

1
4
分享到:
评论

相关推荐

    VOCtrainval_06-Nov-2007.part2.rar

    It is fundamentally a supervised learning learning problem in that a training set of labelled images is provided. The twenty object classes that have been selected are: Person: person Animal: ...

    VOCtrainval_06-Nov-2007.part1.rar

    It is fundamentally a supervised learning learning problem in that a training set of labelled images is provided. The twenty object classes that have been selected are: Person: person Animal: ...

    2013年九年级英语上册 Module 7 Australia Unit 2 The camel that I rode had

    " 需要填写的是名词,结合选项,应该是"sheep",因为sheep单复数同形。 2. "These are your books and those are _________" 这里填入"we"的宾格形式"ours",表示"那些是我们的"。 3. "You’d better use some _____...

    网络流题集

    这类问题在" Pleasant sheep and big big wolf"、"方格取数"以及"Golden Eggs"等题目中出现,它们通常可以通过割点或割边来解决,也可以与最大流问题结合,比如"Key Vertex"和"Being a Hero"。 4. **最大权闭包**:...

    【精品】情景交际基础练习试题和答案解析.doc

    此外,针对不同的情境,如提议、请求、拒绝等,应选择合适的回应,如"Sure, no problem"表示同意,"Sorry, I'm not available now"则表示当前无法满足请求。 2. 习语和俚语:英语中,一些特定的表达方式具有特殊...

    新标准英语六年级英语期末试卷1精选.doc

    4. 没问题 - No problem 5. 看 - Look at 四、图片配对 这部分需要学生将句子与对应的图片连线,例如: 1. 不要在这里右转 - 图片可能与交通标志相关 2. 大象喜欢水 - 图片可能展示大象在水中 3. 上海既大又著名 - ...

    2019_2020学年高中英语Module1BasketballSectionⅡIntroduction&ReadingandV

    11. **单句改错** - "The farmers built fences in order to defend their sheep with wolves."改为"The farmers built fences in order to defend their sheep against wolves." - 正确的介词是"against",表示防御...

    HDU图论题目分类

    * 题目1016:Prime Ring Problem,涉及到递归搜索的概念。 * 题目1026:Ignatius and the Princess I,涉及到完全搜索的概念。 2. 图的遍历:深度优先搜索(DFS)、广度优先搜索(BFS)等。 * 题目1043:Eight,...

    精选_基于JAVA实现的农夫过河问题_源码打包

    文件中会定义类和方法,如`Farmer`、`Wolf`、`Sheep`、`Cabbage`等对象,以及解决问题的核心算法。 2. **状态表示**:在源代码中,农夫、狼、羊和白菜的位置可能用布尔值或者整型变量来表示,分别对应在左岸(0)或...

    九年级英语下册Unit2GreatpeopleWelcometotheunit作业设计新版牛津版202002251149

    (Many sheep were kept by John’s family around the house.) - **比较级的倒装句**:他锻炼得越多,变得就越强壮。(The more exercise he took, the stronger he became.) - **独立主格结构**:我们应该独自...

    21春北京理工大学《综合英语》在线作业参考答案.docx

    5. "turn out" 在 "At first I thought that math problem would be rather difficult, but it turned out to be fairly easy." 中表示“结果是”,表明事情最后并不像最初预想的那样困难。 6. "of you to say so" ...

    甘肃省金塔县第四中学九年级英语 Unit 2练习题(无答案) 人教新目标版

    **词汇**:练习题中涉及到的词汇如`exact`(精确的)、`helpful`(有帮助的)、`donkeys`(驴的复数形式)、`discuss`(讨论)、`original`(原创的)、`cloning`(克隆的动名词形式)、`design`(设计)、`sheep`...

    2年中考1年模拟2016年中考英语专题03冠词试题含解析

    5. 感叹句:用在what引导的感叹句中,如"What a difficult problem!",强调问题的难度。 6. 固定搭配:在特定短语中,如"have a look"意为看一看。 【定冠词the】的用法: 1. 特指:特指已知的人或事物,如"The pen...

    基于Android的哲学家就餐问题源代码

    在计算机科学领域,哲学家就餐问题(Dining Philosophers Problem)是一个经典的多线程同步问题,由艾兹格·迪科斯彻提出,用以阐述并发控制中的死锁问题。在这个问题中,五位哲学家围坐在一张圆桌旁,每人面前有一...

Global site tag (gtag.js) - Google Analytics