20年后农场有多少头牛

i met a interview question while i watch the design pattern vedios of MaShiBing, it's interesting, i wrote them down with the OO design, here it is:

* 农场一头小母牛
* 每年生头小母牛
* 母牛五岁产母牛
* 二十年上多少牛

package com.j2se.Interview.howmanycowsin20years;

import java.util.Collection;
import java.util.HashSet;

public class Farm {
    private Collection<Cow> cows = new HashSet<Cow>();

    public Collection<Cow> getCows() {
        return cows;
    }

    public void addCows(Collection<Cow> newCows) {
        cows.addAll(newCows);
    }

    @Override
    public String toString() {
        return "Farm [cows=" + cows + "]";
    }

}


package com.j2se.Interview.howmanycowsin20years;

public class Cow {
    private int age;

    public Cow(int age) {
        super();
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public boolean isBornable() {
        return age >= 5;
    }

    public Cow lay() {
        return new Cow(0);
    }

    public void grow() {
        age++;
    }

    @Override
    public String toString() {
        return "age=" + age;
    }

}


package com.j2se.Interview.howmanycowsin20years;

import java.util.Collection;

/**
 * 农场一头小母牛
 * 每年生头小母牛
 * 母牛五岁产母牛
 * 二十年上多少牛
 * @author Administrator
 *
 */
public class HowManyCowsIn20Years {

    public static void main(String[] args) {
        Farm farm = new Farm();
        farm.getCows().add(new Cow(0));
        for (int i = 0; i < 20; i++) {
            Collection<Cow> newBornCows = new HashSet<Cow>();
            for (Cow cow : farm.getCows()) {
                if (cow.isBornable()) {
                    newBornCows.add(cow.lay());
                }
                cow.grow();
            }
            farm.addCows(newBornCows);
            System.out.println("第" + (i + 1) + "年共有" + farm.getCows().size() + "头牛:" + farm);
        }
        System.out.println("\nTotal cow in farm after 20 years are : " + farm.getCows().size());
    }

}


output is:
第1年共有1头牛:Farm [cows=[age=1]]
第2年共有1头牛:Farm [cows=[age=2]]
第3年共有1头牛:Farm [cows=[age=3]]
第4年共有1头牛:Farm [cows=[age=4]]
第5年共有1头牛:Farm [cows=[age=5]]
第6年共有2头牛:Farm [cows=[age=0, age=6]]
第7年共有3头牛:Farm [cows=[age=0, age=1, age=7]]
第8年共有4头牛:Farm [cows=[age=1, age=2, age=8, age=0]]
第9年共有5头牛:Farm [cows=[age=2, age=3, age=9, age=1, age=0]]
第10年共有6头牛:Farm [cows=[age=3, age=4, age=10, age=2, age=0, age=1]]
第11年共有7头牛:Farm [cows=[age=4, age=5, age=11, age=0, age=3, age=1, age=2]]
第12年共有9头牛:Farm [cows=[age=5, age=6, age=12, age=1, age=4, age=0, age=0, age=2, age=3]]
第13年共有12头牛:Farm [cows=[age=0, age=6, age=7, age=13, age=2, age=5, age=1, age=1, age=3, age=4, age=0, age=0]]
第14年共有16头牛:Farm [cows=[age=14, age=0, age=4, age=1, age=7, age=0, age=8, age=0, age=0, age=6, age=3, age=2, age=5, age=2, age=1, age=1]]
第15年共有21头牛:Farm [cows=[age=0, age=15, age=1, age=5, age=0, age=0, age=2, age=8, age=0, age=1, age=9, age=1, age=1, age=7, age=4, age=3, age=0, age=6, age=3, age=2, age=2]]
第16年共有27头牛:Farm [cows=[age=1, age=0, age=16, age=0, age=6, age=0, age=0, age=3, age=9, age=0, age=2, age=4, age=3, age=2, age=1, age=1, age=2, age=1, age=10, age=2, age=5, age=8, age=4, age=7, age=1, 
age=0, age=3]]
第17年共有34头牛:Farm [cows=[age=2, age=1, age=17, age=1, age=7, age=1, age=1, age=4, age=10, age=0, age=1, age=3, age=0, age=5, age=0, age=4, age=0, age=0, age=0, age=3, age=0, age=2, age=2, age=3, age=2, 
age=11, age=3, age=6, age=9, age=5, age=8, age=2, age=1, age=4]]
第18年共有43头牛:Farm [cows=[age=0, age=3, age=0, age=2, age=18, age=2, age=8, age=0, age=2, age=0, age=2, age=5, age=11, age=1, age=2, age=0, age=4, age=1, age=6, age=1, age=0, age=5, age=1, age=1, age=1, 
age=0, age=4, age=1, age=0, age=3, age=0, age=3, age=4, age=3, age=12, age=4, age=7, age=10, age=6, age=9, age=3, age=2, age=5]]
第19年共有55头牛:Farm [cows=[age=0, age=1, age=0, age=1, age=3, age=0, age=0, age=9, age=0, age=1, age=3, age=6, age=12, age=2, age=0, age=3, age=1, age=5, age=7, age=2, age=0, age=6, age=1, age=0, age=4, a
ge=0, age=13, age=5, age=11, age=4, age=10, age=3, age=4, age=3, age=0, age=19, age=3, age=1, age=0, age=2, age=1, age=2, age=2, age=0, age=2, age=5, age=2, age=1, age=4, age=1, age=4, age=5, age=8, a
ge=7, age=6]]
第20年共有71头牛:Farm [cows=[age=1, age=0, age=2, age=1, age=2, age=4, age=1, age=1, age=10, age=0, age=1, age=2, age=4, age=0, age=7, age=13, age=3, age=1, age=0, age=4, age=2, age=0, age=6, age=8, age=3, 
age=0, age=1, age=7, age=0, age=2, age=0, age=0, age=1, age=5, age=1, age=0, age=14, age=6, age=12, age=5, age=11, age=4, age=0, age=5, age=0, age=4, age=1, age=20, age=4, age=2, age=0, age=1, age=3, 
age=2, age=3, age=0, age=3, age=1, age=3, age=0, age=6, age=0, age=3, age=2, age=5, age=2, age=5, age=6, age=9, age=8, age=7]]

Total cow in farm after 20 years are : 71

你可能感兴趣的:(20年后农场有多少头牛)