逢三退一实现方法二

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            bool[] arr = new bool[500];
            for (int i = 0; i < arr.Length; i++)
                arr[i] = true;

            int index = 0, leftCount = arr.Length, countNum = 0;
            while (leftCount > 1)
            {
                if (arr[index])
                {
                    countNum++;
                    if (countNum == 3)
                    {
                        arr[index] = false;
                        countNum = 0;
                        leftCount--;
                    }
                }
                index++;
                if (index == arr.Length)
                    index = 0;
            }

            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i])
                {
                    Console.WriteLine(i);
                    break;
                }
            }

            Console.ReadKey();
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            KidCircle kc = new KidCircle(500);
            Console.WriteLine(kc);
            Console.ReadKey();
            Kid k = kc.first;
            int countNum = 0;
            while (kc.count > 1)
            {
                countNum++;
                if (countNum % 3 == 0)
                {
                    //怎样表达当前对象
                    kc.Delete(k);
                    countNum = 0;
                }
                k = k.right;
            }
            Console.WriteLine(kc.first.id);
            Console.ReadKey();
        }
    }

    class KidCircle
    {
        internal int count;
        internal Kid first, last;

        internal KidCircle(int n)
        {
            for (int i = 0; i < n; i++)
                Add();
        }

        internal void Add()
        {
            Kid k = new Kid(); k.id = count;

            if (0 == count)
            {
                first = last = k;
                first.left = first.right = k;
            }
            else
            {
                last.right = k; k.left = last;
                k.right = first; first.left = k;
                last = k;
            }
            count++;
        }

        internal void Delete(Kid k)
        {
            //Console.WriteLine(this.count + "/" + k.id + "/left:" + k.left.id + "/right:" + k.right.id);
            //k.id不在kc里怎么办?

            if (count <= 0)
                return;

            k.left.right = k.right;
            k.right.left = k.left;
            count--;
            if (k.id == last.id)
            {
                last = k.left;
            }
            if (k.id == first.id)
                first = k.right;
        }

        public override string ToString()
        {
            List<string> ls = new List<string>();
            Kid k = first;
            for (int i = 0; i < count; i++)
            {
                ls.Add(string.Format("{0}-Left:{1} Right:{2}", k.id, k.left.id, k.right.id));
                k = k.right;
            }
            return string.Join(Environment.NewLine, ls.ToArray());
        }
    }


    class Kid
    {
        internal int id;
        internal Kid left, right;
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(new Circle(500).FindLast());
            Console.ReadKey();
        }
    }

    public class Circle {
        private Item[] items;
        public Circle(int count) {
            this.items = new Item[count];
            for (int i = 0; i < count; i++)
            {
                this.items[i] = new Item(i);
            }
        }

        public int FindLast() {
            int result = -1;
            
            int count = 0; 
            int selectNo = 0;
            
            while (true)
            {

                if (!this.items[selectNo].Selected) {
                    count++;

                    if (count % 3 == 0)
                    {
                        items[selectNo].Selected = true;
                        count = 0;
                    }

                }

                selectNo++;
                if (selectNo % items.Length == 0)
                    selectNo = 0;
 
                if (this.TotalNotSelected() == 1)
                    break;
            
            }

            for (int i = 0; i < items.Length; i++)
            {
                if (!items[i].Selected)
                {
                    result = i;
                    break;
                }
            }


            return result;
        }

        public int TotalNotSelected() {
            int result = 0;
            for (int i = 0; i < this.items.Length; i++)
            {
                if (!this.items[i].Selected)
                {
                    result++;
                }
            }
            return result;
        }

    }
        
    public class Item {
        public int No { set; get; }
        public bool Selected { set; get; }

        public Item(int no) {
            this.No = no;
        }
    }

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值