手写一波双色球随机生成器,弄着玩的,记录一下(老老实实搬砖,别老想着中彩票)
public static void main(String[] args) {
Random random = new Random();
//双色球红号
int[] red = new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33};
//双色球蓝号
int[] blue = new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
System.out.println("red的长度是:"+red.length);
System.out.println("blue的长度是:"+blue.length);
String strRed = "";
String strBlue= "";
Map<Integer,Integer> redNumber = new HashMap<>();
Map<Integer,Integer> blueNumber = new HashMap<>();
for (int i =0;i< red.length;i++){
redNumber.put(i,red[i]);
if (i<16){
blueNumber.put(i,blue[i]);
}
}
int[] ints = new int[7];
StringBuffer youBlueNumber=new StringBuffer();
for (int i =0;i<7;i++){
Integer index = random.nextInt(32);
while (true){
if (redNumber.get(index)!=null){
ints[i] = redNumber.get(index);
redNumber.remove(index);
break;
}
index = random.nextInt(32);
}
if (i==6){
index= random.nextInt(15);
youBlueNumber.append("你的蓝号为:").append(blueNumber.get(index));
}
}
System.out.println("未排序之前的红号:\n"+ Arrays.toString(ints));
quickSort(ints,0,ints.length-1);
System.out.println("排序之后的红号:\n"+ Arrays.toString(ints));
StringBuffer youLastNuber = new StringBuffer(); //此处引用了大佬的快速排序实现
youLastNuber.append(Arrays.toString(ints)).append(" ").append(youBlueNumber);
System.out.println("随机生成的号码为\n"+youLastNuber.toString());
}
//这里是复制粘贴的大佬代码,自己看了半天才弄懂
public static void quickSort(int[] arr, int left, int right) {
int mid= 0;
if (left < right) {
mid= getMid(arr, left, right);
quickSort(arr, left, mid- 1);
quickSort(arr, mid+ 1, right);
}
}
private static int getMid(int[] arr, int left, int right) {
int key = arr[left];
while (left < right) {
while (left < right && arr[right] >= key) {
right--;
}
arr[left] = arr[right];
while (left < right && arr[left] <= key) {
left++;
}
arr[right] = arr[left];
}
arr[left] = key;
return left;
}
3355

被折叠的 条评论
为什么被折叠?



