2013年9月8日星期日

A title . . .

Farm heifer ,
rope heifers each year ,
old heifers cows ,
two decades later how much cattle ?


java app out there . . .

------ Solution ------------------------------------ --------
you have to consider the bull breeding problems . . .
------ Solution ---------------------------------------- ----
import java.util.ArrayList;

public class TestCow {

/**
 * @param args
 */
public static void main(String[] args) {
ArrayList<Cow> farm = new ArrayList<Cow>();
Cow source = new Cow();
farm.add(source);
// 每年都让农场的所有母牛过一次生日
for (int year = 1; year <= 20; year++) {
// 遍历所有的母牛,让它过生日
for (int i = 0; i < farm.size(); i++) {
Cow mother = farm.get(i);
Cow daughter = mother.birthday();
// 如果生了母牛则加入农场
if (daughter != null) {
farm.add(daughter);
}
}
System.out.println("第" + year + "年,农场规模:" + farm.size());
}
}

}

/**
 * 母牛类
 * 
 * @author mfanw
 * 
 */
class Cow {
/**
 * 刚出生的年龄是0岁
 */
private int age = 0;

public int getAge() {
return age;
}

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

/**
 * 母牛过生日,如果超过5岁则生一个母牛
 * 
 * @return
 */
public Cow birthday() {
age++;
if (age >= 5) {
System.out.println("我超过5岁了,生一个母牛!");
return new Cow();
}
return null;
}

}

------ Solution ------------------------------ --------------
recursive
public class BornOx {
public static void main(String[] args) {
System.out.println(born(1, 6, 20));
}

public static int born(int num, int age, int year) {
if (year <= 0) {
return num;
} else {
if (age >= 5) {
return born(num, age + 1, year - 1) + born(1, 1, year - 1);

} else {
return born(num, age + 1, year - 1);
}
}

}
}

------ Solution ------------------------------------- -------
//农场类
public class Farm {
// 所有的母牛
private List<Cattle> cattles = new ArrayList<Cattle>();

// 初始化农场,最初只有一只5岁的母牛
public Farm() {
Cattle cattle = new Cattle();
cattle.setAge(5);
cattles.add(cattle);
}
// 每过一年,计算出母牛数
public int cattleCountAfterOntYear() {
//遍历所有母牛,判断的没有超过5岁的母牛
for (int i = 0; i < cattles.size(); i++) {
Cattle cattle = cattles.get(i);
//由于过了一年,所以所有的母牛都加了一岁
cattle.increaseAge();
//判断母牛是不是5岁了
if (cattle.getAge() >= 5) {
//母牛生小母牛,初始化小母牛的年龄为0
Cattle newCattle = new Cattle();
cattles.add(newCattle);
}
}
// 返回总母牛数
return cattles.size();
}
public static void main(String[] args) {
Farm cattleCount = new Farm();
//20年后,的母牛数,有点多啊!!!900多。。。。。。
for (int i = 0; i < 20; i++) {
int thisYearNewCattleCount = cattleCount.cattleCountAfterOntYear();
System.out.println("第 " + (i + 1) + " 年的母牛数: "
+ thisYearNewCattleCount + " 头");
}
}
}
// 定义母牛类
class Cattle {
int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// 每过一年,母牛会长一岁
public void increaseAge() {
age++;
}
}

------ For reference only ------------------------- --------------
you have to consider the bull breeding problems . . .  

you can really pull . . . . .
------ For reference only -------------------------------------- -
you have to consider the bull breeding problems . . .          
  
you can really pull . . . . .   Hey hey hey , is being written. . .
------ For reference only -------------------------------------- -
you have to consider the bull breeding problems . . .                
    
you can really pull . . . . .        Hey hey hey , is being written. . .  
my math is not very good ah. . . . They did not come to this theme . . . Oh / pull the nose /
------ For reference only --------------------------------- ------
are ox bar, which can also be side-tracked procedure

没有评论:

发表评论