奶牛问题,别人写的,自己试了一下.
2009-12-09 17:17
218 查看
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace CowCount
7 {
8 public class Cow
9 {
10 public int Age { get; set; }
11 public int Generation { get; set; }
12 public int Id { get; set; }
13 public int ParentId { get; set; }
14
15 public Cow() { }
16
17 public Cow(int age, int generation, int id, int parentId)
18 {
19 this.Age = age;
20 this.Generation = generation;
21 this.Id = id;
22 this.ParentId = parentId;
23 }
24 }
25 class Program
26 {
27 static List<Cow> listCows = new List<Cow>();
28 static int maxYear = 20;
29 public static void GetBirth(int year)
30 {
31 List<Cow> listBornCows = new List<Cow>(); //添加新生的奶牛
32 Cow firstCow = new Cow(1, 1, 1, 0);
33 listCows.Add(firstCow);
34 for (int i = 0; i < year; i++)
35 {
36 foreach (Cow item in listCows)
37 {
38 item.Age++; //年龄自增
39 if (item.Age > 4)
40 {
41 Cow birth = new Cow(1,item.Generation + 1, listCows.Count + 1, item.Id);
42 listBornCows.Add(birth); //添加新生的奶牛
43 }
44 }
45 listCows.AddRange(listBornCows);
46 listBornCows.Clear();
47 }
48 }
49
50 public static List<Cow> GetCowByParentId(int parentId)
51 {
52 List<Cow> result = new List<Cow>();
53 foreach (Cow item in listCows)
54 {
55 if (item.ParentId == parentId)
56 result.Add(item);
57 }
58 return result;
59 }
60
61 public static void ShowCows()
62 {
63 int count = 0;
64 if (listCows != null)
65 {
66 count = listCows.Count;
67 }
68 Console.WriteLine(string.Format("After 20 years,cows count:{0}", count.ToString()));
69
70 //按照所属于母亲显示对应奶牛数
71 int maxParentId = 0;
72 if (listCows.Count > 0)
73 {
74 listCows.Sort(delegate(Cow left, Cow right) { return right.ParentId.CompareTo(left.ParentId); });
75 maxParentId = listCows[0].ParentId;
76 }
77 for (int i = 0; i < maxParentId; i++)
78 {
79 List<Cow> listModels = GetCowByParentId(i);
80 Console.WriteLine(string.Format("Cow_{0}'s children as follows:",i));
81 if(listModels.Count == 0)
82 {
83 Console.WriteLine("Has no any child!");
84
85 }
86 else
87 {
88 foreach (Cow item in listModels)
89 {
90 Console.WriteLine(string.Format("Age:{0},Id:{1},Generation:{2}", item.Age, item.Id, item.Generation));
91 }
92 }
93 }
94 }
95 static void Main(string[] args)
96 {
97 GetBirth(maxYear);
98 Console.WriteLine(listCows.Count);
99 //ShowCows();
Console.ReadLine();
}
}
}
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace CowCount
7 {
8 public class Cow
9 {
10 public int Age { get; set; }
11 public int Generation { get; set; }
12 public int Id { get; set; }
13 public int ParentId { get; set; }
14
15 public Cow() { }
16
17 public Cow(int age, int generation, int id, int parentId)
18 {
19 this.Age = age;
20 this.Generation = generation;
21 this.Id = id;
22 this.ParentId = parentId;
23 }
24 }
25 class Program
26 {
27 static List<Cow> listCows = new List<Cow>();
28 static int maxYear = 20;
29 public static void GetBirth(int year)
30 {
31 List<Cow> listBornCows = new List<Cow>(); //添加新生的奶牛
32 Cow firstCow = new Cow(1, 1, 1, 0);
33 listCows.Add(firstCow);
34 for (int i = 0; i < year; i++)
35 {
36 foreach (Cow item in listCows)
37 {
38 item.Age++; //年龄自增
39 if (item.Age > 4)
40 {
41 Cow birth = new Cow(1,item.Generation + 1, listCows.Count + 1, item.Id);
42 listBornCows.Add(birth); //添加新生的奶牛
43 }
44 }
45 listCows.AddRange(listBornCows);
46 listBornCows.Clear();
47 }
48 }
49
50 public static List<Cow> GetCowByParentId(int parentId)
51 {
52 List<Cow> result = new List<Cow>();
53 foreach (Cow item in listCows)
54 {
55 if (item.ParentId == parentId)
56 result.Add(item);
57 }
58 return result;
59 }
60
61 public static void ShowCows()
62 {
63 int count = 0;
64 if (listCows != null)
65 {
66 count = listCows.Count;
67 }
68 Console.WriteLine(string.Format("After 20 years,cows count:{0}", count.ToString()));
69
70 //按照所属于母亲显示对应奶牛数
71 int maxParentId = 0;
72 if (listCows.Count > 0)
73 {
74 listCows.Sort(delegate(Cow left, Cow right) { return right.ParentId.CompareTo(left.ParentId); });
75 maxParentId = listCows[0].ParentId;
76 }
77 for (int i = 0; i < maxParentId; i++)
78 {
79 List<Cow> listModels = GetCowByParentId(i);
80 Console.WriteLine(string.Format("Cow_{0}'s children as follows:",i));
81 if(listModels.Count == 0)
82 {
83 Console.WriteLine("Has no any child!");
84
85 }
86 else
87 {
88 foreach (Cow item in listModels)
89 {
90 Console.WriteLine(string.Format("Age:{0},Id:{1},Generation:{2}", item.Age, item.Id, item.Generation));
91 }
92 }
93 }
94 }
95 static void Main(string[] args)
96 {
97 GetBirth(maxYear);
98 Console.WriteLine(listCows.Count);
99 //ShowCows();
Console.ReadLine();
}
}
}
相关文章推荐
- 钱币问题(自己没做出来,代码是看别人的改动了一下)
- 这一次不写什么知识博客,就遇到的学习过程中的问题简要概述一下,算是给自己做个归纳总结吧
- samba服务器创立及附上自己和别人的实例及一些无法登陆的问题
- 记录一下自己遇到的问题
- 遇到瓶颈和性能问题,不要掉进别人的坑中,而是想想自己做的话,怎么做。
- 根据别人ruby总结,自己梳理一下
- DIY最近准备配一台经济型的电脑,查了一下配置如下,总价2481元,自己也不专业,不知道有没有问题
- 问答推广之如何通过回答别人的问题来推广自己的网站
- 反思一下自己工作中的几个问题
- 根据别人ruby总结,自己梳理一下
- 自己没才,分享一下别人的
- 关于C中自加/减的问题温习,自己mark一下
- boj 1336 简单的问题 不过自己没想到 别人解释的~~学习~~
- 总结一下自己Java实习后遇到的一些问题。
- 开发中自己遇到的问题和大家分享一下java.io.IOException: tmpFile.renameTo(classFile) failed and(和) failed (2: No suc
- 记录一下自己装IIS遇到的问题
- 月历修改了一下别人的代码自己写的一个新的月历
- 细数一下自己的LINUX目前存在的问题!
- 自己的JAVA基础中的一下乱七八糟的问题(一)
- 拿别人一篇感动人的文章 --- 感动一下自己