zoukankan      html  css  js  c++  java
  • 泛型委托 上海

    最近公司招人,让我出几个上机题,所以就随便到网上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
        }
    }

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

  • 相关阅读:
    ORACLE 查找数据库中有记录的表
    [原]Asp.Net 错误:无法连接到Asp.Net Developement server
    中国移动手机话费查询号码1008611
    动手修改VS2008的解决方案文件,以让VS2005打开它
    [转]飞秋使用说明与常见问题解决方法
    微软发布Fix it 修复Windows 7等系统0day漏洞
    Oracle DECODE 函数应用示例
    [转]C#实现访问网络共享文件夹
    c#保留小数点后位数的方法
    [转]微软紧急修复高危漏洞 30万网民PC已遭攻击
  • 原文地址:https://www.cnblogs.com/luozhai714/p/1174824.html
Copyright © 2011-2022 走看看