您的位置:首页 > 其它

软件大赛题目----(第九个)求牛的数目

2012-01-29 19:16 253 查看


package com.bird.software;

import java.util.ArrayList;
import java.util.List;

public class Cow {
private int age;

public Cow afterYears(){
age++;
return age > 2 ? new Cow() : null;
}

public static void showTotalCowNum(int n){
List<Cow> list = new ArrayList<Cow>();
list.add(new Cow());

for(int i = 0; i < n; i++){
int cowCount = list.size();
for(int j = 0; j < cowCount; j++){
Cow cow = list.get(j).afterYears();
if(cow != null){
++cowCount;
list.add(cow);
}
}
}

System.out.println(n+"年后,共有:"+list.size()+"头牛");
}

public static void main(String[] args){
showTotalCowNum(5);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: