using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Text; namespace ConsoleApplication1 

public class 牛 

public int 月份;//当月份>12且能整除3时,生一头小牛 
public string 出生日期; 
} class Program 

static void Main(string[] args) 

牛 a = new 牛(); 
a.月份 = 12; 
a.出生日期 = "牛祖先"; 
ArrayList arr = new ArrayList(); 
arr.Add(a); 
//开始循环,以月为单位循环 
for (int i = 1; i <= 12 * 10; i++) 

//每个牛的年龄+1 
for (int j = 0; j < arr.Count; j++) 

牛 temp = (牛)arr[j]; 
temp.月份++; 
if (temp.月份 >= 12 && temp.月份 % 3 == 0) 

//生牛 
牛 b = new 牛(); 
b.月份 = -1; 
b.出生日期 = Convert.ToString(i / 12 + 1) + "年" + Convert.ToString(i % 12) + "月"; 
arr.Add(b); 


} //输出牛的数量和每个牛的月份 
//foreach (object o in arr) 
//{ 
// 牛 temp = (牛)o; 
// Console.Write("年龄:{0}月\t",temp.月份); 
// Console.WriteLine("生日:{0}",temp.出生日期); 
//} Console.WriteLine("共计{0}头牛",arr.Count); 



正确的答案有两个一个是119000多,另一个是571000多,两个答案都是正确的

解决方案 »

  1.   


    protected void Page_Load(object sender, EventArgs e)
        {
            Dictionary<int,keyvalue> dic=new Dictionary<int,keyvalue>();
            dic.Add(1,new keyvalue(true,12));
            int num = getsum(0, ref dic);    }
    class keyvalue
        {
           public bool key=false;
           public int _value = 0;
           public keyvalue(bool _key, int __value)
           {
               key = _key;
               _value = __value;
           }
           public keyvalue()
           { }
        }    int getsum(int m, ref Dictionary<int,keyvalue> dic)
        {
            m = m + 3;
            Dictionary<int, keyvalue> newdic = new Dictionary<int, keyvalue>();
            foreach (int key in dic.Keys)
            {
                if (dic[key].key)
                    newdic.Add(newdic.Count + 1, new keyvalue());
                else
                {
                    dic[key]._value = dic[key]._value + 3;
                    if (dic[key]._value > 12)
                        dic[key].key = true;
                }        }
            foreach (int key in newdic.Keys)
            {
                dic.Add(dic.Count + 1, newdic[key]);
            }
            if (m > 120)
                return dic.Count;
            else
                return getsum(m, ref dic);
     
        }
      

  2.   

    给你写个用OO做的例子,纯属参考...只要改写一下Cow类,你甚至可以监控每一头牛的血缘关系...
    public class Cow
    {
        public int Months;//以月为单位的年龄    public event EventHandler Procreate;//产仔事件    public Cow()
        {
            Months = 1;
        }    public void GrowUp()
        {
            if (Months > 12 && Months % 3 == 0)//15个月以后每三个月产一个子母牛
            {
                if (Procreate != null)
                    Procreate(this, new EventArgs());
            }
            Months++;//长一个月
        }
    }static List<Cow> cows = new List<Cow>();static void cow_Procreate(object sender, EventArgs e)
    {
        var cow = new Cow();
        cow.Procreate += new EventHandler(cow_Procreate);
        cows.Add(cow);
    }static void Main()
    {
        var cow = new Cow();
        cow.Months = 15;//第一代母牛从15个月开始
        cow.Procreate += new EventHandler(cow_Procreate);
        cows.Add(cow);
        for (int i = 0; i < 10; i++)//10年
        {
            for (int j = 0; j < 12; j++)//每年12个月
            {
                cows.ForEach(delegate(Cow c) { c.GrowUp(); });//所有牛长一个月
            }
        }
        Console.WriteLine("共有{0}头母牛", cows.Count);
    }共有208621头母牛...
      

  3.   

    我算出来是671 
    private void button1_Click(object sender, EventArgs e)
            {
                  int o=int.Parse(textBox1.Text);            int b = 0;
                int c = 0;
                int k = 0;
                if (o == 1)
                {
                    c = 4;
                }
                else 
                {
                    k = (12 * o - 18) / 3 + 1;
                    for (int i = 1; i <= k; i++)
                    {
                        b += i;
                    }
                    c = b + 12 * o / 3 + 1;
                }  
                    textBox2.Text = c.ToString();
            } 各位大哥看看这个有没有写错