逢三退一实现方法二

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

}

 

寻找最近的2次幂((代码请基于jdk8’联网解析每步) (请详细解析相关概念和描述,联网解析,中文解析)) 在定义elements变量时说,其长度总是2的次幂,但用户传入的参数并不定符合规则,所以就需要根据用户的输入,找到比它大的最近的2次幂。比如用户输入13,就把它调整为16,输入31,就调整为32,等等。考虑下,我们有什么方法可以实现呢? 来看下ArrayDeque是怎么做的吧: private void allocateElements(int numElements) { int initialCapacity = MIN_INITIAL_CAPACITY; // Find the best power of two to hold elements. // Tests "<=" because arrays aren't kept full. if (numElements >= initialCapacity) { initialCapacity = numElements; initialCapacity |= (initialCapacity >>> 1); initialCapacity |= (initialCapacity >>> 2); initialCapacity |= (initialCapacity >>> 4); initialCapacity |= (initialCapacity >>> 8); initialCapacity |= (initialCapacity >>> 16); initialCapacity++; if (initialCapacity < 0) // Too many elements, must back off initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements } elements = new Object[initialCapacity]; } 看到这段迷之代码了吗?在HashMap中也有段类似的实现。但要读懂它,我们需要先掌握以下几个概念: 在java中,int的长度是32位,有符号int可以表示的值范围是 (-2)31 到 231-1,其中最高位是符号位,0表示正数,1表示负数。 >>>:无符号右移,忽略符号位,空位都以0补齐。 |:位或运算,按位进行或操作,1为1。 我们知道,计算机存储任何数据都是采用进制形式,所以个int值为80的数在内存中可能是这样的: 0000 0000 0000 0000 0000 0000 0101 0000 比80大的最近的2次幂是128,其值是这样的: 0000 0000 0000 0000 0000 0000 1000 0000 我们多找几组数据就可以发现规律: 每个2的次幂用进制表示时,只有位为 1,其余位均为 0(不包含符合位) 要找到比个数大的2的次幂(在正数范围内),只需要将其最高位左移位(从左往右第个 1 出现的位置),其余位置 0 即可。 但从实践上讲,没有可行的方法能够进行以上操作,即使通过&操作符可以将某位置 0 或置 1,也无法确认最高位出现的位置,也就是基于最高位进行操作不可行。 但还有个很整齐的数字可以被我们利用,那就是 2n-1,我们看下128-1=127的表示形式: 0000 0000 0000 0000 0000 0000 0111 1111 把它和80对比下: 0000 0000 0000 0000 0000 0000 0101 0000 //80 0000 0000 0000 0000 0000 0000 0111 1111 //127 可以发现,我们只要把80从最高位起每位全置为1,就可以得到离它最近且比它大的 2n-1,最后再执行次+1操作即可。具体操作步骤为(为了演示,这里使用了很大的数字): 原值: 0011 0000 0000 0000 0000 0000 0000 0010 无符号右移1位 0001 1000 0000 0000 0000 0000 0000 0001 与原值|操作: 0011 1000 0000 0000 0000 0000 0000 0011 可以看到最高2位都是1了,也仅能保证前两位为1,这时就可以直接移动两位 无符号右移2位 0000 1110 0000 0000 0000 0000 000
04-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值