Refactoring to Patterns

迎入日月万里风,笑揖清风洗我狂!

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: :: 订阅 订阅 :: 管理 ::
  91 随笔 :: 10 文章 :: 23 评论 :: 1 引用

最近公司招人,让我出几个上机题,所以就随便到网上Copy 几个,发现一个很有意思的问题。
     题目:17个人围成一圈,从第一个人开始报数,报到3的退出,一直到剩下最后一个人,用面向对象的思想去做这道题
   自己费了点功夫,写出来了:
   //======================================================================
   //======================================================================
   

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

namespace ConsoleApplication4
{
    class Program
    {
        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));
                else
                    list.Add(new person(i, "张三" + i.ToString(), 0));

            }
            list.Find(delegate(person p) { return p.ID == 1; }).ParentId = list.Find(delegate(person p) { return p.ID == list.Count; }).ID;
           
        }
        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);
      
        }

    }
    #region

    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;
            }
        }

    #endregion
    }
}

        说老实话,开始,自己写的有点乱。。。。。。。。
 
           

posted on 2008-04-28 16:38  上海-天浩  阅读(...)  评论(...编辑  收藏

Living in ShangHai

Copyright © 2008 天浩阁 Corporation, All Rights Reserved