母牛繁殖问题
question:农场的母牛寿命是5年,母牛第二年和第四年会繁殖母牛一只,第五年死去。现假设农场第一年有一岁母牛一只,问第五年农场有几只牛。
answer:
用面向对象的角度解下。我这里理解的死去那年没有繁殖能力且对配置文件输入的输入没有验证。
母牛类:
?
?
package com.baixing.shanghai.interview;/** * 母牛 * @author zyj * */public class Cow {private int age;// 年龄,从1开始private int lifespan;// 寿命private String fertilizeAges;// 繁殖年龄点,表示方式:",2,4,"private boolean fertilize;// 繁殖能力public Cow() {super();}public Cow(int lifespan, String fertilizeAges) {super();this.age = 1;this.lifespan = lifespan;this.fertilizeAges = fertilizeAges;judgeFertilize(age);}public Cow(int age, int lifespan, String fertilizeAges) {super();this.age = age;this.lifespan = lifespan;this.fertilizeAges = fertilizeAges;judgeFertilize(age);}/** * 年龄加一 * * @return */public int moveTONextyear() {age++;if (age == lifespan) {return lifespan;}judgeFertilize(age);return age;}/** * 设置繁殖能力 * * @param age */public void judgeFertilize(int age) {String strAge = "," + String.valueOf(age) + ",";if (fertilizeAges.indexOf(strAge) == -1) {fertilize = false;} else {fertilize = true;}}public int getAge() {return age;}public void setAge(int age) {this.age = age;judgeFertilize(age);}public String getFertilizeAges() {return fertilizeAges;}public void setFertilizeAges(String fertilizeAges) {this.fertilizeAges = fertilizeAges;}public boolean getFertilize() {return fertilize;}public void setFertilize(boolean fertilize) {this.fertilize = fertilize;}public int getLifespan() {return lifespan;}public void setLifespan(int lifespan) {this.lifespan = lifespan;}}?
?
工厂类:
?
package com.baixing.shanghai.interview;import java.math.BigInteger;import java.util.ArrayList;import java.util.Iterator;import java.util.List;/** * 母牛繁殖类 * @author zyj * */public class BreedFactury {private List<Cow> cows = new ArrayList<Cow>();// 所以母牛private BigInteger cowNUM = BigInteger.ZERO; // 母牛数量private int years ; // 时间:年限private int lifespan; // 寿命private String fertilizeAges;// 繁殖年龄点,表示方式:",2,4,"public BreedFactury() {super();}public BreedFactury(int years, int lifespan, String fertilizeAges) {super();this.years = years;this.lifespan = lifespan;this.fertilizeAges = fertilizeAges;}/** * 规定的年限里繁殖母牛 * * @return */public List<Cow> produce() {// 初始化第一年一头母牛cows.add(new Cow(5, ",2,4,"));cowNUM = cowNUM.add(BigInteger.valueOf(1));for (int i = 2; i <= years; ++i) {//System.out.print("第"+i+"年:");doyear(cows); // 今年要做的事情}return cows;}/** * 今年要做的事情:年龄加一,删除到寿命的牛,繁殖母牛 * * @param cows * @return */public List<Cow> doyear(List<Cow> cows) {// 所有母牛年龄加一for (Cow cow : cows) {cow.moveTONextyear();}// 删除lifespan岁数的母牛Iterator<Cow> iterator = cows.iterator();while (iterator.hasNext()) {Cow cow = (Cow) iterator.next();if (cow.getAge() == 5) {iterator.remove();cowNUM = cowNUM.subtract(BigInteger.valueOf(1));};}// 母牛繁殖int currentAmount = cows.size();for (int i = 0; i < currentAmount; ++i) {if (cows.get(i).getFertilize()) {// 判断有繁殖能力cows.add(new Cow(5, ",2,4,"));cowNUM = cowNUM.add(BigInteger.valueOf(1));}}// 遍历看下每年怒牛数量和年龄// System.out.print("共"+cowNUM.toString()+"只母牛。");//for (Cow cow : cows) {//System.out.print(cow.getAge()+",");//}//System.out.println();return cows;}public List<Cow> getCows() {return cows;}public void setCows(List<Cow> cows) {this.cows = cows;}public BigInteger getCowNUM() {return cowNUM;}public void setCowNUM(BigInteger cowNUM) {this.cowNUM = cowNUM;}public int getYears() {return years;}public void setYears(int years) {this.years = years;}public int getLifespan() {return lifespan;}public void setLifespan(int lifespan) {this.lifespan = lifespan;}public String getFertilizeAges() {return fertilizeAges;}public void setFertilizeAges(String fertilizeAges) {this.fertilizeAges = fertilizeAges;}}?
?
?Junit4类:
?
package com.baixing.shanghai.interview;import java.io.InputStream;import java.util.List;import java.util.Properties;import org.junit.Test;public class ClassTest {@Testpublic void test() throws Exception {InputStream inputStream = ClassTest.class.getResourceAsStream("config.properties");Properties properties = new Properties();properties.load(inputStream);int years = Integer.parseInt(properties.getProperty("years")); // 时间:年限,第几年int lifespan = Integer.parseInt(properties.getProperty("lifespan")); // 寿命String fertilizeAges = properties.getProperty("fertilizeAges");// 繁殖年龄点,表示方式:",2,4,"BreedFactury breedFactury = new BreedFactury(years, lifespan,fertilizeAges);List<Cow> cows = breedFactury.produce();System.out.println(years+"年母牛总数:" + breedFactury.getCowNUM().toString());}}?
?
同一包下的config.properties配置文件
?
years=5lifespan=5fertilizeAges=,2,4,?
输出结果:
?
第2年:共2只母牛。2,1,
第3年:共3只母牛。3,2,1,
第4年:共5只母牛。4,3,2,1,1,
第5年:共7只母牛。4,3,2,2,1,1,1,
5年母牛总数:7
?
?
?
?
?
?
?
?
?
?
?
请问母牛生下来就算一岁么? 请问母牛生下来就算一岁么?package com.cow;import java.util.ArrayList;import java.util.List;public class Cow {private int age;private boolean isDead;private List<Cow> children;public int getAge() {return age;}public void setAge(int age) {this.age = age;}public boolean isDead() {return isDead;}public void setDead(boolean isDead) {this.isDead = isDead;}public List<Cow> getChildren() {return children;}public void setChildren(List<Cow> children) {this.children = children;}@Overridepublic String toString() {return "Cow [age=" + age + ", isDead="+ isDead + "]";}public static void main(String[] args) {Cow cow = new Cow();cow.setAge(1);cow.setChildren(null);cow.setDead(false);List<Cow> cows = new ArrayList<Cow> ();cows.add(cow);CountCow cc = new CountCow();cc.afterManyYeas(10, cows);int count = cc.count(cows);System.out.println(count);cc.print(cows);}}class CountCow{public int count(List<Cow> cows) {int count = 0;if(cows != null && cows.size() > 0){ count = cows.size();for (Cow cow :cows) {if(cow.isDead()){count --;}if (cow.getChildren() != null && cow.getChildren().size() > 0){count += count(cow.getChildren());}}}return count;}public List<Cow> addYear(List<Cow> cows) {List<Cow> result = cows;for (Cow cow : result) {int age = cow.getAge();if (cow.isDead()) {} else if (age + 1 == 5) {cow.setAge(age + 1);cow.setDead(true);} else {cow.setAge(age + 1);}age = cow.getAge();// 如果年龄是2或者4,繁衍一头母牛if (age == 2 || age == 4) {Cow children = new Cow();children.setAge(0);children.setDead(false);if (cow.getChildren() != null && cow.getChildren().size() > 0) {addYear(cow.getChildren());} else if (cow.getChildren() == null) { cow.setChildren(new ArrayList<Cow>());}// 添加为其后代cow.getChildren().add(children);} else {if (cow.getChildren() != null && cow.getChildren().size() > 0) {addYear(cow.getChildren());}}}return result;}public List<Cow> afterManyYeas(int year,List<Cow> cows) {for (int i =0 ; i < year; i++) {addYear(cows);}return cows;}public void print(List<Cow> cows) {for (Cow cow:cows) {System.out.println(cow);if (cow.getChildren() != null && cow.getChildren().size() > 0) {print(cow.getChildren());}}}}
package com.cow;import java.util.ArrayList;import java.util.List;public class Cow {private int age;private boolean isDead;private List<Cow> children;public int getAge() {return age;}public void setAge(int age) {this.age = age;}public boolean isDead() {return isDead;}public void setDead(boolean isDead) {this.isDead = isDead;}public List<Cow> getChildren() {return children;}public void setChildren(List<Cow> children) {this.children = children;}@Overridepublic String toString() {return "Cow [age=" + age + ", isDead="+ isDead + "]";}public static void main(String[] args) {Cow cow = new Cow();cow.setAge(1);cow.setChildren(null);cow.setDead(false);List<Cow> cows = new ArrayList<Cow> ();cows.add(cow);CountCow cc = new CountCow();cc.afterManyYeas(10, cows);int count = cc.count(cows);System.out.println(count);cc.print(cows);}}class CountCow{public int count(List<Cow> cows) {int count = 0;if(cows != null && cows.size() > 0){ count = cows.size();for (Cow cow :cows) {if(cow.isDead()){count --;}if (cow.getChildren() != null && cow.getChildren().size() > 0){count += count(cow.getChildren());}}}return count;}public List<Cow> addYear(List<Cow> cows) {List<Cow> result = cows;for (Cow cow : result) {int age = cow.getAge();if (cow.isDead()) {} else if (age + 1 == 5) {cow.setAge(age + 1);cow.setDead(true);} else {cow.setAge(age + 1);}age = cow.getAge();// 如果年龄是2或者4,繁衍一头母牛if (age == 2 || age == 4) {Cow children = new Cow();children.setAge(0);children.setDead(false);if (cow.getChildren() != null && cow.getChildren().size() > 0) {addYear(cow.getChildren());} else if (cow.getChildren() == null) { cow.setChildren(new ArrayList<Cow>());}// 添加为其后代cow.getChildren().add(children);} else {if (cow.getChildren() != null && cow.getChildren().size() > 0) {addYear(cow.getChildren());}}}return result;}public List<Cow> afterManyYeas(int year,List<Cow> cows) {for (int i =0 ; i < year; i++) {addYear(cows);}return cows;}public void print(List<Cow> cows) {for (Cow cow:cows) {System.out.println(cow);if (cow.getChildren() != null && cow.getChildren().size() > 0) {print(cow.getChildren());}}}}