2017-08-27 55 views
0

我有困難,因爲該程序似乎工作,但被困在開發和顯示最終數組。它應該有45個元素,每個編號爲1-45,但是以隨機順序生成,沒有重複。c#數組超時錯誤

using System; 

    namespace RandomArray 
    { 
     public class RandomArrayNoDuplicates 
     { 
      static Random rng = new Random(); 
      static int size = 45; 
      static void Main() 
      { 
       int [] array = InitializeArrayWithNoDuplicates(size); 
       DisplayArray(array); 
       Console.ReadLine(); 
      } 


      /// <summary> 
      /// Creates an array with each element a unique integer 
      /// between 1 and 45 inclusively. 
      /// </summary> 
      /// <param name="size"> length of the returned array < 45 
      /// </param> 
      /// <returns>an array of length "size" and each element is 
      /// a unique integer between 1 and 45 inclusive </returns> 
      public static int[] InitializeArrayWithNoDuplicates(int size) 
      { 
       int[] arr = new int[size]; 

       for (int i = 0; i < size; i++) 
       { 
        int number = rng.Next(1, size + 1); 
        arr[i] = number; 
        if (i > 0) 
        { 
         for (int j = 0; j <= i; j++) 
         { 
          if (arr[j] == arr[i]) 
          { 

           i = i - 1; 

          } 
          else if (arr[i] != arr[j]) 
          { 
           arr[i] = number; 
          } 
         } 
        } 
       } 
       return arr; 
      } 
      public static void DisplayArray(int[] arr) 
      { 
       for (int x = 0; x < size; x++) 
       { 
        Console.WriteLine(arr[x]); 
       } 
      } 
     } 
    } 

它應該檢查元素來檢查數組中每個元素生成後的重複項。提示更好的方法來解決這個問題?

+3

爲什麼不從數字1-45開始數組然後再加擾呢? –

+0

需要隨機生成每個數字,然後將其添加到數組中。 – PotatoFries

+0

@PotatoFries - 你爲什麼這麼說?它並沒有在「摘要」中說。 – Enigmativity

回答

0

數組中已有的數字越多,其中已有重複的可能性越大。有了最後一個號碼,實際上有44/45的機會得到一個重複。而在最壞的情況下,你必須檢查所有44個元素,甚至找到重複。只有更多數組元素纔會變得更糟。

我只能重複Leonardos建議:用想要的數字創建一個數組。然後擾亂所述數字的順序。我稱之爲「彩票問題」,通常使用兩個List(int)作爲最直觀的。我應該能夠找到一些示例代碼,因爲我經常用它來編寫代碼。

我只是在記事本++中做了這個快速的樣機代碼。可能會混淆長度並計算或搞亂索引,但你應該明白。我希望:

List<int> input = new List<int>(); 
 
List<int> output; 
 

 
//Initialise Input 
 
for(int i = 0; i < 45; i++) 
 
    input[i]=i; 
 
    
 
//Shuffle the array into output 
 
random rng = new Random(); 
 

 
output = new List<int>(input.Lenght); 
 

 
for(; input.Lenght > 0;){ 
 
    int index = rng.NextInt(input.Lenght); 
 
    int value = input(index); 
 
    input.remove(index); 
 
    
 
    output.add(value); 
 
} 
 

 
//output is a fully randomized version of input now
我本來這段代碼在Java中,其中刪除()返回已刪除的元素進行循環的一個內襯裏面的很好的特性。

0

你可以用隨機做到這一點,而不是無序的,但你的推理是錯誤的。在我的示例中,我使用所有可能的數字和另一個使用bools構建數組,最初爲false。每次我使用一個我設置爲true的數字。然後下一次我將Random的範圍減1,並從我的數組中取第n個未使用的值。然後在最後(這是非常多的地方),最後的值不是隨機的 - 它只是最後一個可用的數字。

另一點,你必須種子隨機,否則它不是隨機的!

的代碼看起來是這樣的:

namespace RandomArray 
{ 
    class RandomArrayNoDuplicates 
    { 
     static Random rng = new Random(DateTime.Now.Millisecond); 
     static int size = 45; 

     static void Main(string[] args) 
     { 
      int[] array = InitializeArrayWithNoDuplicates(size); 
      DisplayArray(array); 
      Console.ReadLine(); 
     } 
     /// <summary> 
     /// Creates an array with each element a unique integer 
     /// between 1 and 45 inclusively. 
     /// </summary> 
     /// <param name="size"> length of the returned array < 45 
     /// </param> 
     /// <returns>an array of length "size" and each element is 
     /// a unique integer between 1 and 45 inclusive </returns> 
     public static int[] InitializeArrayWithNoDuplicates(int size) 
     { 
      int[] allNos = new int[size]; 
      bool[] used = new bool[size]; 

      for (int i = 0; i < size; i++) 
      { 
       allNos[i] = i + 1; 
       used[i] = false; 
      } 

      int[] arr = new int[size]; 
      int max = size; 

      for (int i = 0; i < size - 1; i++) 
      { 
       int number = rng.Next(0, max); 
       int ptr = 0; 
       for (int j = 0; j < size; j++) 
       { 
        if (used[j]) 
        { 
         ptr++; 
        } 
        else 
        { 
         if (j == number + ptr) 
         { 
          break; 
         } 
        } 
       } 
       arr[i] = allNos[number + ptr]; 
       used[number + ptr] = true; 
       max--; 
      } 
      for (int i = 0; i < size; i++) 
      { 
       if (used[i] == false) 
       { 
        arr[size - 1] = allNos[i]; 
        break; 
       } 
      } 

      return arr; 
     } 
     public static void DisplayArray(int[] arr) 
     { 
      for (int x = 0; x < size; x++) 
      { 
       Console.WriteLine(arr[x]); 
      } 
     } 
    } 
} 
0

三江源,我一定錯過了概括介紹。嘗試生成數組然後嘗試生成一個具有1/45機率的數組,然後在每個整數之後實現。我應該能夠在最後爭奪陣列。