- 分享
- 0
- 人气
- 0
- 主题
- 2
- 帖子
- 259
- UID
- 311150
- 积分
- 251
- 阅读权限
- 14
- 注册时间
- 2010-4-6
- 最后登录
- 2013-12-9
- 在线时间
- 185 小时
|
本帖最后由 shippo 于 2011-1-13 06:17 PM 编辑
我改了下:- person origin=null;
- person a=new person(origin,"Alan");
- person b=new person(a,"Jam");
- person c =new person(b,"Alex");
- person d=new person(c,"Yash");
-
- person.peoples=new person[10];
- person.peoples[1]=a;
- person.peoples[0]=d;
- person.peoples[2]=c;
- person.peoples[3]=b;
-
- List<person>result=person.GetGrandChild(b);
-
- if(result!=null)
- MessageBox.Show(result[2].Name);
复制代码
- public static List<person> GetGrandChild(person p)
- {
- if(p==null||p.Name==null)
- return null;
-
- List<person> result=new List<person>();
- person dad=p;
- bool noSon=false;
- result.Add(p);
-
- while(!noSon)
- {
- foreach(person pp in peoples)
- {
- if(pp != null && pp.father!=null && pp.father.Name!=null)
- {
- if(pp.father.Name==dad.Name)
- {
- result.Add(pp);
- dad=pp;
- noSon=false;
- break;
- }
- else
- {
- noSon=true;
- }
- }
- }
- }
-
- if(result.Count<3)
- return null;
- else
- {
- result.RemoveRange(3,result.Count-3);
- return result;
- }
- }
复制代码 |
|