一对3对 , a couple, 没性别吗?

解决方案 »

  1.   

    import java.uitl.*;
    class ACoupleOfRabbit{
       int ageInMonth=0;
       static Vector allCouples=new Vector();
       static Vector newBabies=new Vector();
       public void newMonth(){
          ageInMonth++;
          if(ageInMonth>=7){
            if(ageInMonth-7)%4==0){
             newBabies.add(new ACoupleOfRabbit());
            }
            if(ageInMonth==25){
             allCouples.remove(this);
            }
          }
       }   public static void main(String[] args){
          allCouples.add(new ACoupleOfRabbit());
          for(int i=0;i<40;i++){
             for(Enumeration e=allCouples.elements();e.hasMoreElements();){
                ((ACoupleOfRabbit)e.nextElement()).newMonth();
             }
             allCouples.addAll(newBabies);
             Babies.clear();
          }
       }
       System.out.println(allCouples.size());
    }
      

  2.   

    Sorry上面System.out.println,写错位置了
      

  3.   

    老兄 你的程序错误一大堆啊
    第一行就错了 import java.uitl.*; --> import java.util.*;
    > if(ageInMonth-7)%4==0){
    > Babies.clear(); 结果还是 0 ???
    不可能吧我的结果是 1636
    不知对不对
      

  4.   

    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.Iterator;public class Problem
    {
    private static final int totalWeek = 40;

    private static ArrayList allPig = new ArrayList();

    public static void main(String[] args)
    {
    for (int i = 1; i <= totalWeek; i++)
    {
    allPig = new ArrayList();
    allPig.add(new Pig());
    hoggery(i);
    System.out.println("Week " + i + "--Total alive pigs: " + allPig.size() + " pairs.");
    }
    }

    private static void hoggery(int totalMonth)
    {
    //base case:
    if (totalMonth <= 0)
    return;

    //general case:
    LinkedList newLitter = new LinkedList();
    for (Iterator itr = allPig.iterator(); itr.hasNext(); )
    {
    ((Pig)itr.next()).grow(newLitter);
    }
    cleanDead();
    allPig.addAll(newLitter);
    hoggery(totalMonth-1);
    }

    private static void cleanDead()
    {
    for (int i = 0; i < allPig.size(); )
    {
    if ( ((Pig)allPig.get(i)).isDead() )
    allPig.remove(i);
    else
    i++;
    }
    }}class Pig
    {
    private final int lifeCycle = 25;

    private int age = 0;
    private boolean isDead = false;
    public boolean isDead()
    {
    return isDead;
    }

    protected void grow(LinkedList lst)
    {
    if (isDead)
    return;

    age++;
    if (age == lifeCycle)
    {
    isDead = true;
    return;
    }

    if (age == 7 || (age > 7 && age%4 == 3))
    delivery(lst);
    }

    private void delivery(LinkedList lst)
    {
    for (int i = 0; i < 3; i++)
    lst.add(new Pig());
    }
    }
      

  5.   

    hayai(生命树)   太强了,历害