在使用随机生成器

时间:2017-10-20 18:36:25

标签: java arrays

我正在尝试这样做,因此随机生成器不会在数组中生成相同的数字。我也不知道如何找到丢失的号码。我尝试了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 个答案:

答案 0 :(得分:1)

假设:

  • 数字是唯一的
  • 只缺少一个条目
  • 数字范围从[1,10]开始。

解决方案

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

这是O(n)运行时和O(1)空间复杂度。

如果我们打破假设。

您将需要O(N)空间来确定缺少哪些条目。要保留标记,您可以使用ListBitSet或2个字节并手动管理。 N在这里是随机数生成宽度。

答案 1 :(得分:0)

似乎没有提及使用临时数据结构。 您可以对数组进行排序并找到缺少的数字,也可以使用临时排序的数据结构。

答案 2 :(得分:0)

您正在混淆两件事:问题案例的生成器算法和问题本身的解决方案。你不应该对&#34;随机数组&#34;&#34;根本生成(除非你想测试你的解决方案)。你当然不应该尝试编写解决生成样本数组的方法中的问题的代码。

如果您想要一个随机排序的列表,Collections.shuffle将为您处理。如果您想要一个没有单个元素的列表,只需生成所有元素1..n的列表,然后删除随机选择的数字(然后随机播放)。这么多的发电机。至于解决方案,有很多方法可以做到,有人建议使用总和,这是一个非常有效的解决方案。

答案 3 :(得分: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个数字,然后打印其中缺少的数字。

答案 4 :(得分: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
相关问题