• 泛型委托 上海


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

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

  • 相关阅读:
    http协议详解(经典版)
    Sql语句清理日志文件
    Case when用法
    Sql Server 语句美化工具,SQL Pretty Printer Add-In for SSMS
    Ext.Net保存前判断GridPanel中必填项是否为空
    JavaScript中判断对象的值是否为undefined、null
    Your project specifies TypeScriptToolsVersion 3.1, but a matching compiler was not found. The latest available TypeScript compiler will be used (3.3). To remove this warning, install the TypeScript 3.
    gist.github.com 被墙无法访问解决办法
    VSCode 多标签打开文件
    VSCode安装与配置Eslint
  • 原文地址:https://www.cnblogs.com/luozhai714/p/1174824.html
Copyright © 2020-2023  润新知