2017-10-20 114 views
0

我試圖讓它隨機生成器不會在數組中產生相同的數字。我也不知道如何找到丟失的號碼。我嘗試了if語句,它有效,但它重複。在使用隨機生成器的數組中找到丟失的數字

問題問題「在數組中找到缺失的數字,該數組由數字1到10以隨機順序排列,其中一個數字不存在,您必須找到它,使用一個循環。 {5,6,9,4,1,2,8,3,10} - 結果將是:7

import java.util.Random; 


public class questionThree 
{ 
    public static void main(String[] args) 
    { 
    int [] numbers = new int [10]; 
    Random rand = new Random(); 
    int numArr = 1; 

    for (int i = 1; i < 9; i++) 
    { 
     int n = rand.nextInt(10) + 1; 
     numbers[i] = n; 

     if (numbers[i] == numArr) 
     numArr++; 
     else 
     System.out.println("The missing num is " +numArr); 
    } 

    for(int val : numbers) 
    { 
     System.out.println("The next value is " + 
         val); 
    } 
    } 
} 
+5

概括起來,然後你有你的罪魁禍首失蹤。假設沒有重複,只有一個號碼丟失。 'return 55 - sum' –

回答

0

似乎有是對使用臨時數據結構沒有提及 你可以對數組進行排序並找到缺失的數字,或者使用臨時排序的數據結構

+0

這是什麼意思排序數組?像一個arrayList? – Malia

+1

排序在這裏沒有必要。它可以用更便宜的計算方法完成。 –

0

您在混淆兩件事:問題案例的生成器算法和sol解決問題本身。你不應該對如何生成「隨機數組」(除非你想測試你的解決方案)感興趣。你當然不應該做的是嘗試編寫解決問題的代碼,生成示例數組的方法。

如果你想要一個隨機排序的列表,Collections.shuffle將爲你處理。如果你想要一個沒有單個元素的列表,只需生成所有元素的列表1..n,然後刪除隨機選擇的數字(然後洗牌)。發電機非常重要。至於解決方案,有很多方法可以做到這一點,有人已經建議使用總和,這是一個非常有效的解決方案。

1

假設:

  • 號是唯一的
  • 只有一個條目丟失
  • 數爲[1,10]以下。

return 55 - Arrays.stream(yourArr).sum(); 

這與O(N)運行時和O(1)的空間複雜度。

如果我們打破假設。

您將需要O(N)空間來確定哪些條目丟失。要保留標記,您可以使用ListBitSet或2個字節並手動管理它。 N在這裏是隨機數生成寬度。

0

看來你正在尋找這段代碼。

import java.util.Random; 

public class questionThree 
{ 
    public static void main(String[] args) 
    { 
     int [] numbers = new int [9]; 
     Random rand = new Random(); 
     int numArr = 1; 
     numbers[0] = rand.nextInt(10) + 1; 
     for (int i = 1; i < 9; i++) 
     { 
      int n = rand.nextInt(10) + 1; 
      numbers[i] = n; 
      int x =0; 
      while(x<i){ 
       if(numbers[x] == n){ 
        i = i-1; 
        break; 
       } 
       x++; 
      } 

     } 

     int sum = 0; 
     for (int val : numbers) { 
      sum = sum + val; 
      System.out.println("The next value is " + 
        val); 

     } 
     System.out.println("Missing number is " + (55 - sum)); 

    } 
} 

輸出是 -

The next value is 6 
The next value is 2 
The next value is 8 
The next value is 1 
The next value is 4 
The next value is 3 
The next value is 9 
The next value is 10 
The next value is 7 
Missing number is 5 

我生成之間(1〜10)9號隨機然後其數目在它們之間缺少打印。

0

你有兩個選擇:

  • 的方式我做到了,在下面的代碼:設置隨機排列,而不必重複相同的號碼。然後從1到10的for循環,並檢查數組是否存在。你知道1 + 2 + 3 + 2 + 3 + 4 + 5 + 6 + 8 + 9 + 10 = 55.所以,如果你得到數組中所有整數的和,你將有55 - (缺少的數字)。所以現在缺少的數字= 55 - 總和。

這是我做的代碼(第一種方法):

import java.util.Random; 


public class questionThree 
{ 
    public static void main(String[] args) 
    { 
     int [] numbers = new int [9]; 
     Random rand = new Random(); 

     for (int i = 0; i <9; i++) 
     { 
      //setting random numbers in array without repeating 
      numbers[i] = checkForANumber(rand, numbers, i); 

     } 

     //print all nums 
     for(int val: numbers) System.out.println("The next value is " + 
       val); 


     for (int i = 1; i <= 10; i++) 
     { 
      boolean exist = false; 
      for(int val : numbers) 
      { 
       if(val == i){ 
        exist = true; 
       } 

      } 
      if (!exist) System.out.println("The missing number is " + i); 
     } 


    } 

    private static int checkForANumber(Random rand, int[] numbers, int i){ 
     int n = rand.nextInt(10) + 1; 

     boolean NumAlreadyExist = false; 
     for(int j = 0; j < i; j++) 
     { 
      if(numbers[j] == n){ 
       NumAlreadyExist = true; 
      } 
     } 

     if(NumAlreadyExist) return checkForANumber(rand, numbers, i); 
     else return n; 
    } 
} 

輸出:

The next value is 9 
The next value is 3 
The next value is 8 
The next value is 6 
The next value is 7 
The next value is 10 
The next value is 4 
The next value is 2 
The next value is 1 
The missing number is 5