1. 程式人生 > >一組連續的資料,打亂次序後,隨機取出某一個數字,用最簡單的方法查找出取出的數字(求解)

一組連續的資料,打亂次序後,隨機取出某一個數字,用最簡單的方法查找出取出的數字(求解)

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

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

相關推薦

連續資料打亂次序隨機取出一個數字簡單方法取出數字求解

1、對於一組連續的資料,打亂次序後,隨機取出某一個數字(取出數字後,該位置後的所有數字位置前進一位),用最簡單的方法查找出取出的數字。 2、對1的擴充套件,當取出多個數據後,用最簡單的方法查找出取出的數字。 本人開始對於1的情況,將這種場景抽象成通用的模型,跳進了思維陷阱,把2倒是想出來了,暈了....

數字只有一個數字出現其他數字都出現兩次這個數字python

一次 面試 個數字 一個 直接 fas 其中 3.2 != 背景:   電話面試&手撕代碼 2019.03.22 Mufasa 問題:   一串數字中,只有一個數字出現一次,其他數字都出現兩次,查找出這個數字 條件:   這串數字是有序數

簡單方法解決api介面安全問題幾乎無法破解

場景描述 專案需要為第三方提供api服務介面。介面涉及到核心功能,如何保證介面安全。防止偽造身份、篡改資料? 思路 保障資料安全最好的方法,當然是加密了。無法解析內容,自然無法偽造,篡改。 可是使用https證書需要收費的。有其它方法麼? 有的。 訊息雜湊認證(hmac

MDB中高程點的高程值有0值的圖幅遊標遍歷某個字段的值並將到的結果寫入到TXT中

name addm open ces pat message back ext put 1、 mdbs = arcpy.ListWorkspaces("*","Access") 2、 FeatureClasses = arcpy.ListFeatureClasses()

詳解 vue 雙向資料繫結的原理並實現雙向資料繫結

1:vue 雙向資料繫結的原理: Object.defineProperty是ES5新增的一個API,其作用是給物件的屬性增加更多的控制Object.defineProperty(obj, prop, descriptor)引數 obj: 需要定義屬性的物件(目標物件)prop: 需被定義或修改的屬性名(物

編寫一個ArrayList類來儲存1到10之間的數打亂順序輸出按從小到大輸出按從大到小輸出。

/** * Created by whp on 2018/7/30. */ public class Test { public static void main(String[] args) { List list = new ArrayList();

程式設計實現: 資料中只有一個數字出現了次。其他所有數字都是成對出現的。 請這個數字使用位運算

可以在指定陣列中找出只出現一次的元素 #include<stdio.h> int main() { int arr[] = { 1, 3, 4, 3, 1}; int i = 0; int len = sizeof(arr) / sizeof(ar

個數中只有兩個數字是出現其他所有數字都出現了兩次。 這兩個數字程式設計實現。

1.一個數組中只有兩個數字是出現一次,其他所有數字都出現了兩次。 找出這兩個數字,程式設計實現。 #include<stdio.h> #include<stdio.h> int main() { int arr[] = { 1, 3, 8, 1,

段可迴圈輸入數字經過計算輸出的dos批處理程式碼留存參考

技術群裡有新人問這個題目,無聊寫了這個dos批處理。 因為十幾年沒寫dos了,都忘記了,寫這幾行程式碼花了半個小時,所以打算記錄下來,留存備用 @echo off :start set /p mem

程式設計實現: 資料中只有一個數字出現了次。其他所有數字都是成對出現的。 請這個數字

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> int query(int a[], int size){ for (int i = 1; i < size;++i){ a[

【C語言】個數中只有兩個數字是出現其他所有數字都出現了兩次。 這兩個數字程式設計實現。

一看到這道題,我想到了之前學習過的異或。我們知道兩個相同的數字異或的結果是 0,因為在計算機中,異或運算是按照二進位制位來運算的,相同為 0 ,相異為  1。任何數與 0 異或都等於它自己。既然我們要找出來一組數中不同的兩個數字,也可以採用這種方法。 但是這次不是直接異或,

計算離散資料的標準偏差和中位數。函式的構建

% 計算一組離散資料的標準偏差和中位數。 % 我們假設資料是以頻數或資料點數量給出。作為例子,我們再次以辦公室僱員為例 % 子,我們取得每個年齡的僱員數量。假設他們是: % 2 個僱員的年齡是 17 % 1 個僱員的年齡是 18 % 3 個僱員的年齡是 21

個數中只有兩個數字是出現其他所有數字都出現了兩次。這兩個數字

方法1:遍歷,查詢 定義一個臨時變數k=0,不斷將陣列每個數與陣列每個元素比較,如果兩個數相等k++,然後判斷k是否等於1。如果為1,則這個數在陣列只出現一次;如果k=2,說明出現兩次。 #define _CRT_SECURE_NO_WARNINGS 1

個數只有一個數字僅出現其他數字均出現兩次這個數字

要求:不借助任何空間 我們知道:a^0 = a,a^a = 0,因此這裡可以藉助異或運算可以實現。 具體實現如下: public class SingleNum { /**

個數中只有兩個數字是出現其他所有數字都出現了兩次。這兩個數字程式設計實現。

#include<stdio.h> Find_Num(int arr[], int sz , int * num1, int *num2) { * num1 = 0; * num2 = 0; int i = 0;

【C語言】個數中只有兩個數字是出現其他所有數字都出現了兩次。 這兩個數字

要求:一個數組中只有兩個數字是出現一次,其他所有數字都出現了兩次。 找出這兩個數字,程式設計實現。程式設計實現。 程式碼如下 int find(int arr[],int len) { int i,j,count; for(i = 0;i

個數中只有兩個數字是出現其他所有數字都出現了兩次。 這兩個數字程式設計實現。

思路: 1.我們都知道如果兩個相同的數進行異或會相互抵消結果為0,所以我們先將數組裡的所有數進行異或,得到的結果是陣列中兩個只出現一次的不同的數的異或結果(記為ret) 2.ret的二進位制數中,是

C++對pair資料進行排序(sort函式的使用

最近在寫一個演算法的時候,把一些資料存在了pair中,並且需要根據pair中first或者second的值對這些資料進行排序。比如:輸入資料(1,2)、(4,2)、(3,3)、(2,1)根據first的值大小進行升序排序,輸出(1,2)、(2,1)、(3,3)、(4,2)。經過思索之後得到的實現方法如下:首先

個數中只有兩個數字是出現其他所有數字都出現了兩次。 這兩個數字程式設計實現。

# include <stdio.h> # include <windows.h> void find_once(int arr[], int len) { int i = 0; int j = 0; for

資料中只有一個數字出現了次。 其他所有數字都是成對出現的。請這個數字陣列指標的方法

(一)思考思路 例: 一組資料中只有一個數字出現了一次 資料:arr[]={1 ,3 ,5 ,7, 1, 3, 5}這組資料中,只有7出現了一次。 結構:找到的數就是:7. 1:對於這樣的一個數組,我們應該使用函式呼叫的辦法來實現,使得整個程式清晰可見 2:要找出這樣的數,