1、对于一组连续的数据,打乱次序后,随机取出某一个数字(取出数字后,该位置后的所有数字位置前进一位),用最简单的方法查找出取出的数字。
2、对1的扩展,当取出多个数据后,用最简单的方法查找出取出的数字。
本人开始对于1的情况,将这种场景抽象成通用的模型,跳进了思维陷阱,把2倒是想出来了,晕了....
大家也可以先试着写写,以下的代码并不是最好的(纯粹的测试代码),只作为参考而已。
对于1的代码如下:
class="code_img_closed" src="/Upload/Images/2013071810/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('c88d6fec-1ce1-4100-b8c0-a804a0b864b3',event)" src="/Upload/Images/2013071810/2B1B950FA3DF188F.gif" alt="" />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
大家有好的想法写出来,学习学习...