您将如何制作仅包含 1-1000 值的 10000 数组? [英] How would you make an array of 10000 with only values of 1-1000 inclusive?

查看:36
本文介绍了您将如何制作仅包含 1-1000 值的 10000 数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个函数都应该使用大小为 100、1000 和 10000 的数组输入运行;其中每个
任何数组中的值都应该是 1 – 1000 之间的整数.每个排序函数都应该
在以下类型的数组上运行:随机数、排序列表和几乎排序列表"

"Each of the functions should be run with array inputs, of size 100, 1000 and 10000; where each
value in any array should be an integer from 1 – 1000 inclusive. Each sorting function should be
run on arrays of the following types: random numbers, sorted lists and almost sorted lists"

下面我创建了三个数组.

Below I have created three arrays.

第一个用 1-1000 的整数随机填充 10000 的数组.

First one fills Array of 10000 randomly with integers from 1-1000.

第二次用 1-10000 的整数填充 10000 的数组.第三个 10000 的 Shuffles 数组,其中包含 1-10000 的整数.

Second fills array of 10000 with integers from 1-10000. Third Shuffles array of 10000 which include integers from 1-10000.

我的问题是我无法让我的第二个和第三个 10000 数组只包含 1-1000 的值.甚至有可能吗?我是新手.任何帮助将不胜感激!!

My problem is I can't get my 2nd and 3rd Array of 10000 to only include values from 1-1000. Is is even possible? I'm new to this. Any help will be appreciated!!

int [] inputTenThousand = new int[10000];               // Random 10000
    for (int a = 0; a < inputTenThousand.length; a++) {
       inputTenThousand [a] = (int) (Math.random () * 1000);
    }



int [] inputTenThousand2 = new int[10000]               // Sorted 10000
    for (int a = 0; a < inputTenThousand2.length; a++) {
       inputTenThousand2[a] = a + 1;
    }




List<Integer> TenThousandList = new ArrayList<Integer>();
    for (int i = 1; i < 10001; i++) {
        TenThousandList.add(i);
    }
     Collections.shuffle(TenThousandList);
int[] inputTenThousand3 = new int[TenThousandList.size()];  // Almost Sorted 10000
    for (int i = 0; i < TenThousandList.size(); i++) {
       inputTenThousand3[i] = TenThousandList.get(i);
    }
    for (int i = 0; i < inputTenThousand3.length; i++) {            
       inputTenThousand3[i] = TenThousandList.get(i);   
    }

推荐答案

您可以非常接近地使用您已有的代码,只需添加 模运算符 用于第二个和第三个列表.添加元素mod 1000"可确保列表中没有大于 1000 的值.(您必须在结果值上加 1 以将范围从 0-999 上移到 1-1000).

You can come very close using the code you already have, by just adding the modulo operator for the second and third lists. Adding elements "mod 1000" ensures that you have no values in the list greater than 1000. (You have to add one to the resulting values to shift the range up from 0-999 to 1-1000).

inputTenThousand2[a] = (a % 1000) + 1;

当然,这不会保留您最初创建的排序顺序,但是一旦您生成这些数组,您会注意到一个非常清晰的模式.您的数组现在只是数字 1-1000,重复十次.这使得想象排序后的数组看起来很容易:

Of course, this doesn't preserve the sorted order you created originally, but once you generate these arrays, you'll notice a very clear pattern. Your array is now just the numbers 1-1000, repeated ten times. This makes picturing what the array would look like sorted very easy:

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
 ...
 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000] 

所以,我们可以首先构造一个漂亮的排序列表:

So, we can just construct that nice sorted list in the first place:

int [] inputTenThousand2 = new int[10000];     // 10000 sorted integers
for (int v = 0; v < 1000; v++) { // loop from 0 to 999
    for (int i = 0; i < 10; i++) { 
        inputTenThousand2[(10*v) + i] = v + 1; // Set ten elements per value of the outer loop
    }
}

然后您可以将此列表复制到第三个列表中,并为您的第三个案例稍微取消排序"!

You can then copy this list into a third list and "slightly unsort" it for your third case!

当然,根据您可以访问的内容(这看起来像是一个作业,所以也许您没有现成的排序),创建第一个列表可能会更容易,因为您已经拥有它,然后复制它并为第二种情况排序.

Of course, depending on what you have access to (this looks like an assignment, so maybe you don't have sorting readily available), it's likely to be easier to just create the first list as you have it already, then copy it and sort it for the second case.

这篇关于您将如何制作仅包含 1-1000 值的 10000 数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆