今天碰到一个奶牛的问题.一头奶牛从出生后的第四年开始生育小牛,以后每年生育一头.
给你一头刚出生的小奶牛,20年后共有多少头奶牛!!
希望大家给出算法和思路!我先谢谢了!

解决方案 »

  1.   

    奶牛也学会分裂生殖了...这应该算...进化吧
    struct Cow
    {
    Age=0;
    }
    ArrayList cos=new ArrayList();
    Cow co1;void Mcast()
    {cos.Add(co1);
    for(int x=0;x<20;x++)
    {
    ArrayList newC = new ArrayList();
    foreach(object o in cos)
    {
    ((Cow)o).Age++;
    if(((Cow)o).Age>=4)
    newC.Add(co1);
    }
    foreach(object o in newC)
    cos.Add(o);
    }
    }
    最后就应该是cos.Length了
      

  2.   

    如果新出生的奶牛也能生奶牛的话那么
    最初的奶牛可以生
    20-4=16头生出的奶牛,其中从第13头开始没有生育机会
    所以
    1+2+3+……+12=78最终:16+78=94根据该计算结果推出其基本模型为
    设年限为n 隔m年开始有生育能力,后每年生o头则
    (n-m)*o+(1+2+3+……+(n-m))*o=o*(1+2+3+……+2(n-m))最终我们可以编写以下代码:(类C风格)int cow(int n,int m,int o){
      int sum=0;
      for(int i=1;i<=n-m;i++){
         sum+=i;
      }//end for;
      sum+=i;
      return sum*o;
    }//end function;
      

  3.   

    上面搞错了!!! class Cow
    {
    private static ArrayList cows;
    private int age;

    static Cow()
    {
    cows=new ArrayList();
    } public Cow()
    {
    age=1;
    } public int Age
    {
    get{return age;}
    } public static Cow operator ++(Cow c)
    {
    c.age++;
    return c;
    } public static int CowPropagate(int n)
    {
    Cow cow=new Cow();
    Cow.cows.Add(cow);
    for(int year=1;year<=n;year++)
    {
    int count=Cow.cows.Count; for(int j=0;j<count;j++)
    {
    Cow c=((Cow)Cow.cows[j]);
    if(c.Age>=4)//4岁以上的成年奶牛繁殖新的奶牛
    {
    Cow.cows.Add(new Cow());
    }//end if;
    c++;//每头奶牛年龄长大1岁
    }//end for;
    }//end for;
    return Cow.cows.Count;
    }
    } class CowWork
    {
    static void Main(string[] args)
    {
    int n=20;
    System.Console.WriteLine("20年后共有奶牛:{0}头",Cow.CowPropagate(n));
    }
    }下面正确
      

  4.   

    这个题目非常之不严谨,没说有没有交配就生殖,也没有说奶牛到什么时候不能生,也没有说生的奶牛都是母牛!!!!!一定要推理,应该是下面模型(1^表示一头小于4岁的奶牛)
    年  奶牛数
    -----------------------
    4   1 + 1^
    5   2 + 1^
    6   3 + 1^
    7   4 + 1 + 1^
    8   5 + 2 + 1^
    9   6 + 3 + 1^
    10  7 + 4 + 1 + 1^
    11  8 + 5 + 2 + 1^
    ...第n年的奶牛数是:
    (n - 3) + (n - 6) + ... + (n - k) + ( 一直到(n - k) <=0时,最后补1结束  )故20年后,奶牛数 = 
    17 + 14 + 11 + 8 + 5 + 2 + 1 = 58
      

  5.   


    int GetCow(int year)
    {
     if (year<4)
        return 1;int sum = 0;
    int n = year;
       while(n>0)
       {
           n -= 3;       
           sum += (n > 0 ) ? n : 1;      
        }
    return sum;
    }
      

  6.   

    using System;namespace ConsoleApplication1
    {
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>

    public class TipCalculator 
    {
    const int MAXYEAR=20;
    const int YEAR=4;
    const int START=1;
    public static int Main(string[] args) 
    {
    int [] cow =new int[MAXYEAR]; //数组内存放每年出生的奶牛数
    int i,j,sum=0;
    cow[0]=START;
    for (i=1;i<MAXYEAR;i++) //计算当年出生的奶牛数,存入数组
    {
    cow[i]=0;
    for(j=0;j<i-YEAR;j++)
    {
    cow[i]+=cow[j];
    }
    }
    for (i=0;i<MAXYEAR;i++)//全部相加就是奶牛总数
    sum+=cow[i];
    Console.WriteLine("{0}",sum);
    return 0;
    }
    }
    }最后是106
      

  7.   

    看错题了,是第四年生不是四年后生,要改一下这里
    for(j=0;j<i-YEAR+1;j++)
    最后算出来是250头
    给出每年出生的情况
    1
    0
    0
    1
    1
    1
    1
    2
    3
    4
    5
    7
    10
    14
    19
    26
    36
    50
    69最后加起来是250头
      

  8.   

    public class Cow{
    private int age;
    public Cow( int intAge ){
    age=intAge;
    } public int Age{
    set{
    age=value;
    }
    get{
    return age;
    }
    } public bool CanBirth(){
    if(age==0)
    return false;
    if(age >= 4)
    return true;
    else
    return false;
    }
    }
    static void Main() 
    {
                               ArrayList stable=new ArrayList();
    int count=0;
    Cow cow=new Cow(0);
    stable.Add(cow);
    for(int iyear=0;iyear<20;iyear++){ count=stable.Count;
    for(int i=count-1;i>=0;i--){
    Cow mycow=(Cow)stable[i];
    mycow.Age++;
    if(mycow.CanBirth())
    {
    Cow newcow=new Cow(0);
    stable.Add(newcow);
    }
    }
    } MessageBox.Show(stable.Count.ToString());


    }
    答案是345
      

  9.   

    类似波非那奇数列的问题.
    a[1] = 1;a[2] = 1;a[3] = 1;
    a[n] = a[n-1] + a[n-3]; (n>3) 
      

  10.   

    用上面那个公式递归做法.
    public int funtion(int i)
    {
        if(i<4)
        {
            return 1;
        }
        else
        {
            return funtion(i-1)+funtion(i-3);
        }
    }
      

  11.   


    这个题目非常之不严谨,没说有没有交配就生殖,也没有说奶牛到什么时候不能生,也没有说生的奶牛都是母牛!!!!!一定要推理,应该是下面模型(1^表示一头小于4岁的奶牛)
    年  奶牛数
    -----------------------
    4   1 + 1^
    5   2 + 1^
    6   3 + 1^
    7   4 + 1 + 1^
    8   5 + 2 + 1^
    9   6 + 3 + 1^
    10  7 + 4 + 1 + 1^
    11  8 + 5 + 2 + 1^
    ...第n年的奶牛数是:
    (n - 3) + (n - 6) + ... + (n - k) + ( 一直到(n - k) <=0时,最后补1结束  )故20年后,奶牛数 = 
    17 + 14 + 11 + 8 + 5 + 2 + 1 = 58
      
    同意这个算法 但数据有点出入1 1
    2 1
    3
    4 1
    5 2 1
    6 3
    7 4 1
    8 5 2 1
    9 6 3
    10 7 4 1
    11 8 5 2 1
    12 9 6 3
    13 10 7 4 2 1
    14 11 8 5 3
    15 12 9 6 4 1
    16 13 10 7 5 2 1
    17 14 11 8 6 3
    18 15 12 9 7 4 1 66
      

  12.   

    也就是说20年后应该是66头牛.不好意思,是用excel加出来的.:-)