农场一头老母牛,每年生头小母牛,母牛5岁生母牛,二十年上多少牛。请用面向对象的思维来做这道题

解决方案 »

  1.   

    建个母牛类,母牛类包含年龄属性,和年龄增长方法。建个农厂类,一个队列,成员类型为母牛,一个增加母牛方法,一个母牛数量成员。一个农村年龄成员,一个农场年龄增长方法。主函数是个for循环,每循环一次代表一年。年龄++,每5年向队列追加母牛。队列的长度就是母牛数量。
      

  2.   


    public class Cow {
    private final int MUM_AGE = 5;
    private int mAge; public Cow(int age) {
    mAge = age;
    } public int getAge() {
    return mAge;
    } public void setAge(int age) {
    mAge = age;
    } public boolean canHasBaby(){
    return mAge >= MUM_AGE;
    }
    }
    import java.util.ArrayList;public class Test {
    public static void main(String[] args) {
    System.out.println("count = " + getSum(20));
    } static int getSum(int year){
    ArrayList<Cow> mumcows = new ArrayList<Cow>();//能生育的母牛
    mumcows.add(new Cow(5));//老母牛
    ArrayList<Cow> childCows = new ArrayList<Cow>();//小母牛
    for(int i = 0;i < year;i++){
    childCows.clear();
    for(int j = 0;j < mumcows.size();j++){
    Cow cow = mumcows.get(j);
    cow.setAge(cow.getAge() + 1);//每年长一岁
    if(cow.canHasBaby()){
    childCows.add(new Cow(0));//添加一只0岁母牛
    }
    }
    mumcows.addAll(childCows);
    }
    return mumcows.size();
    }
    }
      

  3.   

    是个特殊的Fibonacci 数列。
    有谁能在面向对象的基础上用Fibonacci 的算法做出来吗?
      

  4.   


    public class Cow {
    /**
     * 计数器
     */
    static long count=1 ;
    /**
     * 年龄
     */
    int age;
    /**
     * 母牛的孩子
     */
    private List<Cow> children = new ArrayList<Cow>();
    Cow(){

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

    public void burn(Cow cow ,int year){
    for(Cow child : cow.children){
    burn(child,year);
    child.setAge(child.age+1);
    }
    if(cow.age > 4){
    cow.children.add(new Cow());
    count++;
    }
    cow.setAge(cow.age+1);
    }

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

    public static void yearsLate(Cow cow ,int yearsAgo){
    for(int i = 0 ; i < yearsAgo; i ++){
    cow.burn(cow, i);
    }
    }

    public static void main(String[] args) {
    Cow cow = new Cow(5);
    yearsLate(cow, 6);
    System.out.println(cow.count);
    }

    }
      

  5.   

    以下是牛的代码/**
     * 牛
     */
    public class Cow { public int age = 0;
    /**
     * 所在的农场
     */
    public Farm farm;

    /**
     * 长大一岁
     */
    public void grow() {
    age++;
    if (age > 5) {//大于5岁了就生一个牛然后还给农场
    Cow cow = new Cow();
    farm.comeBack(cow);
    }
    }
    }以下是农场的代码/**
     * 农场 
     */
    public class Farm { public List<Cow> cows = new ArrayList<Cow>();

    /**
     * 一年又一年啊!
     */
    public void yearByYear() {
    for (int i = 0; i < 20; i++) {
    for (Cow cow : cows) {
    cow.grow();
    }
    }
    }
    /**
     * 归还一头牛
     * @param cow 牛
     */
    public void comeBack(Cow cow) {
    cow.farm = this;
    cows.add(cow);
    }


    }
    随便找个地方写main函数吧,以下是运行代码Farm farm = new Farm();
    Cow cow = new Cow();
    cow.age = 20;//一头老牛
    farm.comeBack(cow);
    farm.yearByYear();
    System.out.println(farm.cows.size());
    为了减少代码量,没有写setter和getter,全写成了public的了。但是现在有个问题,我的代码运行不了,报错了,好像是线程安全有关的,因为归还小牛的时候,牛集合还在遍历。不解,求教
      

  6.   

    #include <string>
    #include <iostream>
    #include <cstdlib>
    #include <list>using namespace std;/**
     * 在面向对象之前先用面向过程的思想解决了这个问题
     *//* 1.需要一个表示农场的链表来圈养牛。
     * 2.对于牛只要有一个表示其年龄的属性即可。
     * 3.需要一个检查链表中牛的年龄的方法来在合适的时候生小牛
     * 4.生小牛的方法负责给农场添加一只小小牛。
     * 5.在主函数中组合这些顺序,打印结果
     *//**
     * @brief: 生一头小牛并放到农场链表中
     * @p_farm_list: 农场链表指针 
     * @return value: 返回-1表示流产,返回0表示喜得母牛(恭喜农场)
     */
    static int born_a_cow(list<unsigned int*>* p_farm_list);/**
     * @brief: 让公牛fuck所有的母牛,看看他们是不是能生
     * @p_farm_list: 要fuck的农场
     * @years: 每年让公牛fuck母牛一次,fuck多少年
     * @return value: 返回-1表示农场没开门或者今年不fuck了,返回0表示成功受精,
     * 但是母牛可能连大姨妈都没有呢,所以未必生小牛
     */
    static int fuck_all_cow(list<unsigned int*>* p_farm_list,unsigned int years);int main(int argc, const char *argv[])
    {
    list<unsigned int*> farm_list;/* 农场,用来放牛 */
    unsigned int old_cow;/* 农场的老母牛 */
    unsigned int years;
    /* 检测外部参数 */
    if (argc < 2) {
    cout << "usage: " << argv[0] <<  " years" << endl;
    return -1;
    }
    years = (unsigned int)atoi(argv[1]);
    old_cow = 5;/* 假装母牛5岁了,正直发情期,这个程序怎么没有公牛出现*/
    /* 1.把老母牛放进农场准备fuck(说的文明点叫交配)*/
    farm_list.push_back(&old_cow);
    /* 2.开始每年一度的fuck盛宴,连续fuck 20年。母牛好能生 */
    fuck_all_cow(&farm_list,years);
    /* 3.数数20年后多少牛 */
    cout << "经历了" << years << "年的不懈奋斗,农场现有母牛 "<< farm_list.size() << "头。" << endl;
    /*4.清理内存 */
    for (list<unsigned int *>::iterator it = farm_list.begin(); it != farm_list.end();){
    if(*it) {
    it=farm_list.erase(it);
    }
    else
    ++it;
    } return 0;
    }static int born_a_cow(list<unsigned int*>* p_farm_list)
    {
    if (!p_farm_list) {
    return -1;
    }
    unsigned int* pCow = new unsigned int;
    *pCow = 1;
    p_farm_list->push_back(pCow);
    return 0;
    }static int fuck_all_cow(list<unsigned int*>* p_farm_list, unsigned int years)
    {
    if (!p_farm_list || !years) {
    return -1;
    }
    list<unsigned int*>::iterator it;
    while (years) {
    for (it = p_farm_list->begin(); it != p_farm_list->end(); ++it) {
    if (**it >= 5) {
    born_a_cow(p_farm_list);
    }
    ++(**it);
    }
    --years;
    }
    }
      

  7.   

    #include <string>
    #include <iostream>
    #include <cstdlib>
    #include <list>using namespace std;/**
     * 此时所谓的面向对象只不过是对上面方法流程的一个封装而已
     */
    class Farm
    {
    public:
    Farm(unsigned int y) : years(y){
    unsigned int old_cow = 5;
    farm_list.push_back(&old_cow);
    fuck_all_cow();
    }
    ~Farm(void){
    for (list<unsigned int *>::iterator it = farm_list.begin(); it != farm_list.end();){
    if(*it) {
    it=farm_list.erase(it);
    }
    else
    ++it;
    }
    }
    inline unsigned int  how_much_cow(void){
    return farm_list.size();
    } private:
    int born_a_cow(void);
    int fuck_all_cow(void);
    private:
    unsigned int years;
    list<unsigned int*> farm_list;/* 农场,用来放牛 */
    };
    int Farm::born_a_cow(void)
    {
    unsigned int* pCow = new unsigned int;
    *pCow = 1;
    farm_list.push_back(pCow);
    return 0;
    }int Farm::fuck_all_cow(void)
    {
    list<unsigned int*>::iterator it;
    while (years) {
    for (it = farm_list.begin(); it != farm_list.end(); ++it) {
    if (**it >= 5) {
    born_a_cow();
    }
    ++(**it);
    }
    --years;
    }
    }int main(int argc, const char *argv[])
    {
    unsigned int old_cow;/* 农场的老母牛 */
    unsigned int years;
    /* 检测外部参数 */
    if (argc < 2) {
    cout << "usage: " << argv[0] <<  " years" << endl;
    return -1;
    }
    years = (unsigned int)atoi(argv[1]);
    Farm a_farm(years);
    cout << "经历了" << years << "年的不懈奋斗,农场现有母牛 "<< a_farm.how_much_cow() << "头。" << endl; return 0;
    }
      

  8.   

    package com.gudii.countcow;
    public class Cow {
    private int age = 0;
    private final int bemumage=5;
    public  boolean CanHaveBaby(int age){
    return (age>=bemumage);

    }
    public void setAge(int age) {
    this.age = age;
    }
    public int getAge() {
    return age;
    }
    }
    ------
    package com.gudii.countcow;
    import java.util.ArrayList;
    public class CowHouse {
    public static void main(String[] args) {
    ArrayList<Cow> mumcow=new ArrayList<Cow>();
    Cow mcow =new Cow(); 
    mcow.setAge(5);
    mumcow.add(mcow);
    ArrayList<Cow> childcow=new ArrayList<Cow>();
    for(int i=0;i<1;i++){
    for(int j=0;j<mumcow.size();j++){ //拿出每头母牛
    Cow mc = mumcow.get(j);
    mc.setAge(mc.getAge() + 1);
    if(mc.CanHaveBaby(mc.getAge())){
    Cow ccow =new Cow();
    childcow.add(ccow);
    }
    }
    mumcow.addAll(childcow);
    childcow.clear();
    }
    System.out.println("count =" + mumcow.size() );
    mumcow.clear();
    }
    }
      

  9.   

    17楼说的很清楚了 
    不过貌似集合在遍历中是不能添加元素的,所以这样了。
    可以改成这样for(年份=2012 to 2032){
        int 新生牛 = 0;
        for(母牛: 所有母牛){
           新生牛 += 母牛.可以生小牛么();
        }
        
        农场.新增小牛(新生牛);
    }
      

  10.   

    母牛类:
    package com.cw.farm;public class Cow {

    private int age;

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

    public void grow(){
    this.age++;
    }

    /*
     * 母牛生小牛
     */
    public Cow reproduced(){
    /*
     * 如果母牛年龄大于等于5就返回一个0岁的小母牛,否则返回空
     */
    if(this.age>=5){
    return new Cow(0);
    }else{
    return null;
    }
    }}农场类:
    package com.cw.farm;import java.util.ArrayList;
    import java.util.List;public class Farm {
    /**
     * @param cows 用来存放所有的牛的集合
     * @param children 用来暂时存放每一年产生的小母牛
     */
    List<Cow> cows = new ArrayList<Cow>();
    List<Cow> children = new ArrayList<Cow>();

    /**
     * 
     * @param year 指明第几年后
     */
    public void timePast(int year){
    for(int i=0;i<year;i++){
    for(Cow c :cows){
    c.grow(); //年龄增长函数
    //如果本年度该母牛可以生牛,就把所生的小母牛添加到children集合中
    if(c.reproduced()!=null){
    children.add(c.reproduced());
    }
    }
    cows.addAll(children);
    children.clear();

    }

    }
    public static void main(String[] args) {
    Farm farm = new Farm();
    Cow cow = new Cow(5);
    farm.cows.add(cow);
    farm.timePast(19);
    System.out.println("cow number is:"+farm.cows.size());

    }}
    计算结果是:cow number is:325
      

  11.   

    import java.util.List;public class 母牛 { public 母牛(int 年龄) {
    super();
    this.年龄 = 年龄;
    } @Override
    public String toString() {
    return "母牛 [年龄=" + 年龄 + "]";
    } private int 年龄 = 0; public void 长大一岁() {
    年龄 += 1;
    } public int 今年几岁() {
    return 年龄;
    } public 母牛 MakeSubCow() {
    return new 母牛(0);
    }
    }import java.util.ArrayList;
    import java.util.List;public class 大农场 {
    public static void main(String[] args) {
    开始运作农场();
    } public static void 开始运作农场() {
    List<母牛> 牛总数 = new ArrayList<母牛>();
    // 默认是个大母牛
    牛总数.add(new 母牛(10));
    for (int i = 0; i < 20; i++) {
    for (母牛 牛牛 : 牛总数) {
    牛牛.长大一岁();
    }
    List<母牛> temp = new ArrayList<母牛>();
    for (母牛 牛牛 : 牛总数) {
    if (牛牛.今年几岁() >= 5) {
    temp.add(牛牛.MakeSubCow());
    }
    }
    牛总数.addAll(temp);
    }
    System.out.println(牛总数.size());
    }
    }
      

  12.   

    类似Fibonacci思路做,用递推式...
      

  13.   


    哦,应该是431,应该用farm.timePast(20);
      

  14.   


    哦,应该是431,应该用farm.timePast(20);嗯,我这算出来也是431~挺有意思的问题!
      

  15.   

    哈哈 
    斐波那契数列么 一搜一大堆 http://blog.csdn.net/zqfddqr/article/details/5553661
      

  16.   

    public static void main(String[] args) {
    List<Cow> cows = new ArrayList<Cow>();

    Cow oldCow = new Cow(6);

    cows.add(oldCow);

    for(int i =1;i<=20;i++){
    for(int j =0;j<cows.size();j++){
    Cow tempCow = cows.get(j);
    if(tempCow.getAge()>5){
    Cow newCow = new Cow(1);
    cows.add(newCow);
    }
    cows.get(j).setAge(tempCow.getAge()+1);
    }
    }
    System.out.println(cows.size());

    }外加一个Cow类。请问我这算法有问题吗。。结果是431.
      

  17.   

    #include<iostream>
    using namespace std;
    void main()
    {
    int a[20];
    int i;
    for(i=0;i<5;i++)
    {
    a[i]=1;
    for(i=5;i<20;i++)
    {
    a[i]=a[i-1]+a[i-3];
    }
    cout<<a[19]<<endl;
    }
      

  18.   

    #include<iostream>
    using namespace std;
    void main()
    {
    int a[20];
    int i;
    for(i=0;i<5;i++)
    {
    a[i]=1;
    }
    for(i=5;i<20;i++)
    {
    a[i]=a[i-1]+a[i-3];
    }
    cout<<a[19]<<endl;
    }
      

  19.   

    Cow(){
      static List list;
      int age = 0;
      Cow(){
        addCow(this);
      }
      void incAge(){
         age++;
         if(age >=5){
             new Cow();
         }
      }  static void addCow(Cow cow){
         list.add(cow);
      }
    }
      

  20.   

    无聊,写个public class Test1 {
    public static void main(String[] args) {
    Farm f = new Farm();
    for (int i = 1; i < 21; i++) {
    f.live(Cow.GIVE_BIRTH_CYCLE);
    }
    System.out.println(f.getCowsCount() - 1);
    }
    }class Cow {
    public final static int GIVE_BIRTH_CYCLE = 1;//生育周期
    public final static int GIVE_BIRTH_START = 5;//开始生育年龄
    private int age = 0;
    public Cow(int age) {
    this.age = age;
    }
    public void live(int cycle) {
    this.age += cycle;
    }
    public Cow giveBirth() {
    if (this.age >= GIVE_BIRTH_START) {
    return new Cow(0);
    }
    return null;
    }
    }class Farm {
    private List<Cow> cows = new ArrayList<Cow>();
    public Farm() {
    this.cows.add(new Cow(Cow.GIVE_BIRTH_START));
    }
    public void live(int cycle) {
    List<Cow> calfs = new ArrayList<Cow>();
    Cow calf = null;
    for (Cow cow : this.cows) {
    cow.live(cycle);
    calf = cow.giveBirth();
    if (calf != null) {
    calfs.add(calf);
    }
    }
    addAllCow(calfs);
    }
    public void addAllCow(List<Cow> cow) {
    this.cows.addAll(cow);
    }
    public int getCowsCount() {
    return cows.size();
    }
    }
      

  21.   

    public class Test1 {
    public static void main(String[] args) {
    Farm f = new Farm();
    for (int i = 1; i < 21; i++) {
    f.live(Cow.GIVE_BIRTH_CYCLE);
    }
    System.out.println(f.getCowsCount() - 1);
    }
    }class Cow {
    public final static int GIVE_BIRTH_CYCLE = 1;//生育周期
    public final static int GIVE_BIRTH_START = 5;//开始生育年龄
    private int age = 0;
    public Cow(int age) {
    this.age = age;
    }
    public void live(int cycle) {
    this.age += cycle;
    }
    public Cow giveBirth() {
    if (this.age >= GIVE_BIRTH_START) {
    return new Cow(0);
    }
    return null;
    }
    }class Farm {
    private List<Cow> cows = new ArrayList<Cow>();
    public Farm() {
    this.cows.add(new Cow(Cow.GIVE_BIRTH_START));
    }
    public void live(int cycle) {
    List<Cow> calfs = new ArrayList<Cow>();
    Cow calf = null;
    for (Cow cow : this.cows) {
    cow.live(cycle);
    calf = cow.giveBirth();
    if (calf != null) {
    calfs.add(calf);
    }
    }
    addAllCow(calfs);
    }
    public void addAllCow(List<Cow> cow) {
    this.cows.addAll(cow);
    }
    public int getCowsCount() {
    return cows.size();
    }
    }