h********t 发帖数: 189 | 1 Modify the code below so that the generate method does not create duplicate
numbers.You should add additional static methods, rather than put all the
code inside the existing loop.
下面是CODE:
import java.util.Random;
public class Part1
{
/**
* There are 5 numbers in each row
* (Similar to the 5 numbers under each letter in Bingo)
*/
public static final int TOTAL_NUMBERS = 5;
/**
* The smallest number is 1
*/
public static final int MIN_NUMBER |
B*****g 发帖数: 34098 | 2 不太明白,难道不是要一个check dup的function?
duplicate
【在 h********t 的大作中提到】 : Modify the code below so that the generate method does not create duplicate : numbers.You should add additional static methods, rather than put all the : code inside the existing loop. : 下面是CODE: : import java.util.Random; : public class Part1 : { : /** : * There are 5 numbers in each row : * (Similar to the 5 numbers under each letter in Bingo)
|
A**o 发帖数: 1550 | 3 应该不用,弄一个1到15序列,从里面随机挑,挪到新的位置就可以了。
【在 B*****g 的大作中提到】 : 不太明白,难道不是要一个check dup的function? : : duplicate
|
h********t 发帖数: 189 | 4 please give more detailed, thanks a lot!!! |
z****e 发帖数: 54598 | 5 /**
* This method populates an array a with numbers between MIN_NUMBER and
* MAX_NUMBER
*
* At present, it does not check for duplicates.
*/
public static void generate(int[] a) {
Set s = new HashSet();
Random r = new Random();
for (int i = 0; i < TOTAL_NUMBERS; i++) {
/**
* NB: r.nextInt returns a number that is >= 0 and < MAX_NUMBER
*
* So this statement generates a num |
z****e 发帖数: 54598 | 6 看了一下题目要求
感觉有些不太明白
只能在fixme那个地方加代码吗?
还是在main函数里面加一个静态方法?
如果是后者,检查出来有重复的数字,又该怎么办呢?
重新取? |
h********t 发帖数: 189 | 7 Yes, you can only modify from "fixme"
Thank you so much, I appreciate your help!! |
B*****g 发帖数: 34098 | 8 not insert array, reset indicator - 1?
【在 z****e 的大作中提到】 : 看了一下题目要求 : 感觉有些不太明白 : 只能在fixme那个地方加代码吗? : 还是在main函数里面加一个静态方法? : 如果是后者,检查出来有重复的数字,又该怎么办呢? : 重新取?
|
A**o 发帖数: 1550 | 9 另一种办法:
public static void generate(int[] a) {
Random r = new Random();
List list = new ArrayList(MAX_NUMBER);
for (int i = 0; i < MAX_NUMBER; i++) {
list.add(i + 1);
}
for (int i = 0; i < TOTAL_NUMBERS; i++) {
int size = list.size();
int index = r.nextInt(size);
a[i] = list.get(index);
list.remove(index);
}
} |
w*****d 发帖数: 2415 | 10 Can't you keep a Set object as the instance field and store all the previous
random numbers in the set? And whenever, a new number is generated, check
whether it is already in the set or not. Sounds to me quite a
straightforward question.
duplicate
【在 h********t 的大作中提到】 : Modify the code below so that the generate method does not create duplicate : numbers.You should add additional static methods, rather than put all the : code inside the existing loop. : 下面是CODE: : import java.util.Random; : public class Part1 : { : /** : * There are 5 numbers in each row : * (Similar to the 5 numbers under each letter in Bingo)
|
c*****t 发帖数: 1879 | 11 This problem is a FAQ. Go to the programming board and read JHQ.
public static void generate (int[] a)
{
int size = MAX_NUMBER - MIN_NUMBER + 1;
int[] tmp = new int[size];
// initiate the numbers for swapping
for (int i = 0; i < size; ++i)
tmp[i] = MIN_NUMBER + i;
Random r = new Random();
// do the swapping
for (int i = 0; i < TOTAL_NUMBERS; ++i)
{
int swapIndex = r.nextInt (size - i);
a[i] = tmp[i + swapIndex];
tmp[i + swapIndex] = tmp[i];
}
} |
l*******g 发帖数: 4894 | 12 我觉得zhaoce的方法已经可以了。
题目要求写一个静态方法调用来查看,那就每次去check这个
public static int checkUnqiue(int[] a, i)
{
List list = Arrays.asList(a);
Set set = new HashSet(list);
if(set.size()
{
System.out.println("have duplicate");
i--;
}
return i;
}
然后再generate 的循环里面调用这个去控制i,这个不好的地方就是产生了好多没有用的内存对象,对erformance有影响。
不让改循环的话。那就把上面的代码的i--改成曲调用generate,然后把返回改成void。这样在GENERATE之后去掉用茶看是否有duplicate,如果有就重新生成,比较不好的是这样很难得到满意的数,毕竟是random。所以不过无论怎么做,循环都是要改的。我觉得zhaoce的方法已经 |
F****n 发帖数: 3271 | 13 正解coconut已经给出了。
【在 l*******g 的大作中提到】 : 我觉得zhaoce的方法已经可以了。 : 题目要求写一个静态方法调用来查看,那就每次去check这个 : public static int checkUnqiue(int[] a, i) : { : List list = Arrays.asList(a); : Set set = new HashSet(list); : if(set.size(): { : System.out.println("have duplicate"); : i--;
|
l*******g 发帖数: 4894 | 14 糊糊,coconut的方法真的不错,用过的就不用重复用了。
【在 F****n 的大作中提到】 : 正解coconut已经给出了。
|