Circle逢3退出问题

//
/*
 * 17个人围成一圈,从第一个人开始报数,报到3的退出,最后剩下几
 
*/

using System;
using System.Collections.Generic;
using System.Text;

namespace Design_Pattern.Observer
{
    
class circle2
    
{
        
static void Main(string[] args)
        
{

            PersonList psl 
= new PersonList();
            psl.InitList();
            psl.Play();
            psl.Show();

            Console.ReadLine();

        }

    }

    
class PersonList
    
{
        List
<person> list = new List<person>();

        
public void InitList()
        
{

            
for (int i = 1; i <= 17; i++)
            
{
                
if (i > 1)
                
{
                    
//list.Add(new person(i, "张三" + i.ToString(), list.Find(delegate(person p) { return p.ID == i - 1; }).ID));
                    list.Add(new person(i, "Hello" + i.ToString(), list[i-2].ID));
                   
// Console.WriteLine(list[i]);
                }

                
else
                
{
                    list.Add(
new person(i, "Hello" + i.ToString(), 0));
                    
                }

                
//Console.WriteLine(list[i]);

            }

            
//list.Find(delegate(person p) { return p.ID == 1; }).ParentId = list.Find(delegate(person p) { return p.ID == list.Count; }).ID;
            list[0].ParentId = list[16].ParentId;
            

        }


        
/// <summary>
        
/// 最后剩下的两个数字
        
/// </summary>

        public void Show()
        
{
            
foreach (person prs in list)
            
{
                Console.WriteLine(
"ID:" + prs.ID);
                Console.WriteLine(
"Name:" + prs.Name);
                Console.WriteLine(
"ParentId:" + prs.ParentId.ToString());
            }


        }

        
public void Play()
        
{
            
int Intex = 2;

            
// 无限循环
            for (; ; )
            
{
                
if (list.Count >= 3)
                
{

                    Remove(Intex);

                    Intex 
+= 2;
                    
if (Intex >= list.Count)
                        Intex 
= Intex - list.Count;

                }

                
else
                
{
                    
break;
                }

            }

        }

        
private void Remove(int index)
        
{
            
int index1 = index, index2 = index;
            
if (index == this.list.Count - 1)
                index1 
= -1;
            
if (index == 0)
                index2 
= this.list.Count;

            list[
++index1].ParentId = list[--index2].ID;
            list.RemoveAt(index);

        }


    }



    
class person
    
{
        
int id;
        
string name;
        
int parentid;

        
public person()
        
{

        }

        
public person(int id, string name, int parentId)
        
{
            
this.id = id;
            
this.name = name;
            
this.parentid = parentId;
        }

        
public int ID
        
{
            
get
            
{
                
return id;
            }

            
set
            
{
                id 
= value;
            }

        }

        
public string Name
        
{
            
get
            
{
                
return name;
            }

            
set
            
{
                name 
= value;
            }


        }

        
public int ParentId
        
{
            
get
            
{
                
return parentid;
            }

            
set
            
{
                parentid 
= value;
            }

        }

    }

}

posted @ 2008-05-29 14:37  晓岚  阅读(158)  评论(0)    收藏  举报