题目:1.假设有一个题库,里面存放着1000道题目(题型都一样)现在我们要从这题库里,随机抽取100道不同的题生成一张考卷请描述你的思路,选出题号(整数)即可
贴上解法,这种思路是通过产生一个对照随机数组,然后对随机数数组排序进而将原试卷数组顺序打乱,最后取出前100道题目,而这100道是完全随机的!当然也有简单的解法,这只是一个稍微复杂的想法,纯属写着玩!
public class Test { public static void main(String... args) { Test test = new Test(); int [] paperIds = test.getPaperID(); System.out.println("试卷编号:"+Arrays.toString(paperIds)); int [] randomArrays = test.getRandomArray(); System.out.println("对照组随机数编号:"+Arrays.toString(randomArrays)); int[] randomPapers = new int[100]; int temp =0; int atemp = 0; for (int i = 0; i < randomPapers.length; i++) { for (int j = 0; j < randomPapers.length-i-1; j++) { if (randomArrays[i]>randomArrays[j]) { temp = randomArrays[j]; randomArrays[j] = randomArrays[i]; randomArrays[i] = temp; //对随机数组排序的时候也就打乱了试卷的顺序。保证了试卷的完全随机 atemp = paperIds[j]; paperIds[j] = paperIds[i]; paperIds[i] = atemp; } } } System.out.println(Arrays.toString(randomArrays)); System.out.println(Arrays.toString(paperIds)); for (int i = 0; i < randomPapers.length; i++) { randomPapers[i] = paperIds[i]; } System.out.println("最后筛选出的卷子编号:"+Arrays.toString(randomPapers)); } //获取卷子的数组 private int[] getPaperID(){ int temp[] = new int[1000]; for (int i = 1; i < temp.length+1; i++) { temp[i-1] = i; } return temp; } //随机一个跟卷子数组一样长度的数组 private int[] getRandomArray(){ int temp[] = new int[1000]; for (int i = 0; i < temp.length; i++) { temp[i] = new Random().nextInt(99999999); } return temp; } }最后附上结果: