1、對於一組連續的資料,打亂次序後,隨機取出某一個數字(取出數字後,該位置後的所有數字位置前進一位),用最簡單的方法查找出取出的數字。

2、對1的擴充套件,當取出多個數據後,用最簡單的方法查找出取出的數字。

本人開始對於1的情況,將這種場景抽象成通用的模型,跳進了思維陷阱,把2倒是想出來了,暈了....

大家也可以先試著寫寫,以下的程式碼並不是最好的(純粹的測試程式碼),只作為參考而已。

對於1的程式碼如下:

        public static int? FindDeletedOneElement(int totalLength, int firstValue, int exchangeCount)
        {
            if (totalLength <= 0)
            {
                return null;
            }

            int[] array = CreateArray(totalLength, firstValue);

            int length = array.Length;
            int[] arrayCopy = new int[length];
            array.CopyTo(arrayCopy, 0);

            Exchange(ref array, exchangeCount);

            Random random = new Random();
            int randomIndex = random.Next(0, length);

            DeleteRandomIndexData(ref array, randomIndex);

            int deletedValue = 0;
            int arrayResultLength = array.Length;

            for (int index = 0; index < length; index++)
            {
                if (index < arrayResultLength)
                {
                    deletedValue -= array[index];
                }

                deletedValue += arrayCopy[index];
            }

            Output(arrayCopy, "Copy Array:");
            Output(array, "Original Array:");
            Output(new List<int>() { deletedValue }, "DELETED VALUES:");

            return deletedValue;
        }
. FindDeletedOneElement

對於2的程式碼如下:

        private static IList<int> FindDeletedElements(int totalLength, int firstValue, int exchangeCount, int deleteCount)
        {
            if (totalLength <= 0)
            {
                return null;
            }

            int[] array = CreateArray(totalLength, firstValue);

            int length = array.Length;
            int[] arrayCopy = new int[length];
            array.CopyTo(arrayCopy, 0);

            Exchange(ref array, exchangeCount);
            DeleteData(ref array, deleteCount);


            int?[] arrayFilled = new int?[length];

            for (int index = 0; index < array.Length; index++)
            {
                int currentValue = array[index];
                int position = currentValue - firstValue;
                arrayFilled[position] = currentValue;
            }

            IList<int> deletedValues = new List<int>();

            for (int index = 0; index < length; index++)
            {
                if (arrayFilled[index] == null)
                {
                    deletedValues.Add(index + firstValue);
                }
            }

            Output(arrayCopy, "Copy Array:");
            Output(array, "Original Array:");
            Output(deletedValues, "DELETED VALUES:");

            return deletedValues;
        }
FindDeletedElements

所有程式碼如下:

    class Program
    {
        static void Main(string[] args)
        {
            int firstValue = -40;
            int totalLength = 20;

            FindDeletedOneElement(totalLength, firstValue, 12);

            FindDeletedElements(totalLength, firstValue, 10, 7);

            Console.ReadLine();
        }

        private static IList<int> FindDeletedElements(int totalLength, int firstValue, int exchangeCount, int deleteCount)
        {
            if (totalLength <= 0)
            {
                return null;
            }

            int[] array = CreateArray(totalLength, firstValue);

            int length = array.Length;
            int[] arrayCopy = new int[length];
            array.CopyTo(arrayCopy, 0);

            Exchange(ref array, exchangeCount);
            DeleteData(ref array, deleteCount);


            int?[] arrayFilled = new int?[length];

            for (int index = 0; index < array.Length; index++)
            {
                int currentValue = array[index];
                int position = currentValue - firstValue;
                arrayFilled[position] = currentValue;
            }

            IList<int> deletedValues = new List<int>();

            for (int index = 0; index < length; index++)
            {
                if (arrayFilled[index] == null)
                {
                    deletedValues.Add(index + firstValue);
                }
            }

            Output(arrayCopy, "Copy Array:");
            Output(array, "Original Array:");
            Output(deletedValues, "DELETED VALUES:");

            return deletedValues;
        }

        private static void DeleteData(ref int[] array, int count)
        {
            if (array == null || array.Length == 0
                || count < 1 || count > array.Length)
            {
                return;
            }

            Random random = new Random();

            for (int index = 0; index < count; index++)
            {
                int randomIndex = random.Next(0, array.Length);

                DeleteRandomIndexData(ref array, randomIndex);
            }
        }

        public static int? FindDeletedOneElement(int totalLength, int firstValue, int exchangeCount)
        {
            if (totalLength <= 0)
            {
                return null;
            }

            int[] array = CreateArray(totalLength, firstValue);

            int length = array.Length;
            int[] arrayCopy = new int[length];
            array.CopyTo(arrayCopy, 0);

            Exchange(ref array, exchangeCount);

            Random random = new Random();
            int randomIndex = random.Next(0, length);

            DeleteRandomIndexData(ref array, randomIndex);

            int deletedValue = 0;
            int arrayResultLength = array.Length;

            for (int index = 0; index < length; index++)
            {
                if (index < arrayResultLength)
                {
                    deletedValue -= array[index];
                }

                deletedValue += arrayCopy[index];
            }

            Output(arrayCopy, "Copy Array:");
            Output(array, "Original Array:");
            Output(new List<int>() { deletedValue }, "DELETED VALUES:");

            return deletedValue;
        }

        private static void Output(IList<int> deletedValues, string title)
        {
            if (deletedValues == null)
            {
                return;
            }

            StringBuilder builder = new StringBuilder();
            builder.AppendLine(title);

            foreach (int value in deletedValues)
            {
                builder.Append(value + "  ");
            }

            builder.AppendLine();

            Console.Write(builder);
        }

        private static void Exchange(ref int[] array, int count)
        {
            if (array == null || array.Length <= 1 || count <= 0)
            {
                return;
            }

            Random randomIndex = new Random();

            for (int index = 0; index < count; index++)
            {
                int startIndex = randomIndex.Next(0, array.Length);
                Thread.Sleep(100);
                int endIndex = randomIndex.Next(0, array.Length);

                if (startIndex == endIndex)
                {
                    continue;
                }

                int tempValue = array[startIndex];
                array[startIndex] = array[endIndex];
                array[endIndex] = tempValue;
            }
        }

        private static void DeleteRandomIndexData(ref int[] array, int randomIndex)
        {
            if (array == null)
            {
                return;
            }

            int length = array.Length;

            if (randomIndex < 0 || randomIndex >= length)
            {
                return;
            }

            for (int index = randomIndex; index < length - 1; index++)
            {
                array[index] = array[index + 1];
            }

            array = array.Take(length - 1).ToArray();
        }

        private static int[] CreateArray(int count, int startValue)
        {
            if (count <= 0)
            {
                return new int[0];
            }

            int[] array = new int[count];

            for (int index = 0; index < count; index++)
            {
                array[index] = startValue;
                startValue++;
            }

            return array;
        }
    }
Program

大家有好的想法寫出來,學習學習...