你将如何使10000阵列具有包容性1-1000只值? [英] How would you make an array of 10000 with only values of 1-1000 inclusive?

查看:84
本文介绍了你将如何使10000阵列具有包容性1-1000只值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的功能,每个人都应该有阵列投入运行,大小为100,1000和10000;其中每个结果
在任何数组值应为整数1 - 1000以下。每个排序功能应搜索
下列类型的数组运行:随机数排序的列表和几乎有序列表

下面我创建了三个数组。

首先一个来自1-1000填补与10000的整数数组随机

二填充从1-10000整数数组10000。
第三搅乱10000,其中包括从1-10000的整数数组。

我的问题是我不能让10000我的第二和第三阵列只包括从1-1000值。
是甚至有可能?我是新来这个。任何帮助将AP preciated!

  INT [] = inputTenThousand新INT [10000] //随机10000
    对于(int类型的= 0; A< inputTenThousand.length; A ++){
       inputTenThousand [α] =(int)的(的Math.random()* 1000);
    }INT [] = inputTenThousand2新INT [10000] //排序10000
    对于(int类型的= 0; A< inputTenThousand2.length; A ++){
       inputTenThousand2 [α] = A + 1;
    }
清单<整数GT; TenThousandList =新的ArrayList<整数GT;();
    的for(int i = 1; I< 10001;我++){
        TenThousandList.add(ⅰ);
    }
     Collections.shuffle(TenThousandList);
INT [] = inputTenThousand3新INT [TenThousandList.size()]; //几乎排序10000
    的for(int i = 0; I< TenThousandList.size();我++){
       inputTenThousand3 [I] = TenThousandList.get(ⅰ);
    }
    的for(int i = 0; I< inputTenThousand3.length;我++){
       inputTenThousand3 [I] = TenThousandList.get(ⅰ);
    }


解决方案

您可以非常接近使用code你已经有了,只需添加的第二和第三列出模运算符。添加元素MOD 1000,确保您在列表中大于1000(您必须添加一个结果值转移从0-999到1-1000的范围内了)。

没有值

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

当然,这并不preserve您最初创建的排序顺序,但一旦你产生这些数组,你会发现一个很明显的模式。阵列现在只是数字1-1000,重复十次。这使得想象的阵列将是什么样子排列非常简单:

  [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]

那么,我们就可以建设摆在首位,那该多好排序列表:

  INT [] = inputTenThousand2新INT [10000] // 10000整数排序
对于(INT V = 0; V< 1000; v ++){//循环从0到999
    对(INT I = 0; I&小于10;我++){
        inputTenThousand2 [(10 *ⅴ)+ I] = V + 1; //设置每个外部循环的值十个元素
    }
}

您可以那么这个列表复制到第三个列表,稍微不排序,它为你的第三个案例!


当然,这取决于你所拥有的访问(这看起来像一个赋值,所以也许你没有选现成的),它可能更容易刚刚创建的第一个列表,您有它已经,那么复制和排序它的第二种情况。

"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.

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

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

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);   
    }

解决方案

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;

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.

这篇关于你将如何使10000阵列具有包容性1-1000只值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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