我試圖生成一個隨機的組合列表。每個組合都有一個從1到6的數字,以及一個來自cc(z,aa,bb,cc)的字母。當號碼再次出現時,該信將成爲下一個。創建一個組合數組,檢查其中的一個項目
例如:
ID | COMBINATION
1A = 5,1,2
2A = 2,2,1
3A = 1,3,1
4A = 1,4,3
5A = 1,5,4
6A = 1,6,4
1B = 9,1,2
是隨機生成的組合的列表,並檢查最後的3項,所以該組合將不再重複。我得到的是這樣的:
3416352645234156342561456235
我不代碼添加字母,但它會是這樣的:
3A 4A 1A 6A 3B 5A 2A 6B 4B 5B 2B 3C 4C 1B
等。
好吧,現在我想要檢查以前的組合的代碼,並檢查最後3個組合的第一個數字是否與當前組合不同。
所以,如果所生成的組合是:
3A = 1,3,1
4A = 1,4,3
1A = 5,1,2
6A = 1,6,4
3B = 2,3,1
的組合標識是不同的,因此是好的,但組合3A和4A的第一項是相同的,所以這將是連續的和我不不需要它。
在這種情況下,圖4A將與圖4B所取代,這將是這樣的:
4B = 2,4,3
然後有如圖1A所示,其中第一項是比圖4B和圖3A不同的,所以是好的。
但是那裏有6A,其中第一項與3A的第一項相同。因此,這將與6B被替換,這將是:
6B = 2,6,4
但隨後6B第一個項目,是一樣的4B,所以它會與6C所取代,這將是:
6C = 5,6,4
但後來6C的第一項與1A相同,所以它會換成6D ......
依此類推。
最後,將讀取跳過的項目。因此,在最後的情況下,4A和6A被替換,這意味着當使用4CC時,下一個將被加入4A,並且當使用6CC時,下一個將被加入6A。直到每個組合(每個數字29)都將被使用。
我不知道,如果你讓我,但如果你不這樣做,以爲它涉及到另外一個問題之前發表評論。我已經做了大量的研究,但沒有成功。
這裏是我當前的代碼,以及組合的圖片。 在代碼中,我嘗試創建一個組合類,但我沒有關於實現我想要的邏輯的想法。
代碼:
import java.io.*;
import java.util.*;
public class Randomizer {
public static int[] one = { 5, 9, 11 },
two = { 2, 3, 5, 6, 10, 11, 13, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 13, 1, 2, 6, 7, 9, 10, 11, 12 },
three = { 1, 2, 4, 1, 2, 3, 7, 8, 1, 2, 4, 6, 7, 8 }, four = { 1, 2, 4, 5, 6 },
five = { 1, 2, 5, 6, 9, 11, 12 }, six = { 1, 2, 5, 6, 9, 11, 12 };
public static int posuno = 0, posdos = 0, postres = 0, poscuatro = 0, poscinco = 0, posseis = 0;
public static void main(String[] args) {
int[] nums = new int[2000];
for (int i = 0; i < nums.length; i++) {
Integer[] arr = new Integer[6];
for (int j = 0; j < arr.length; j++) {
arr[j] = j + 1;
}
Collections.shuffle(Arrays.asList(arr));
for (int j = 0; j < arr.length; j++) {
if (i < nums.length) {
nums[i] = arr[j];
}
}
}
String numbers = Arrays.toString(nums);
numbers = numbers.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll(",", "").replaceAll(" ", "");
StringBuilder solution = new StringBuilder();
int[] nextValidPos = { -1, -1, -1, -1, -1, -1 };
int pos = 0;
while (solution.length() < 203) {
int nextNumber = Integer.valueOf(numbers.substring(pos, pos + 1));
if (nextValidPos[nextNumber - 1] <= solution.length()) {
solution.append(nextNumber);
nextValidPos[nextNumber - 1] = solution.length() + 3;
if (nextNumber == 1)
nextValidPos[nextNumber - 1] += 4;
}
pos++;
}
// show(solution.toString());
int[] list = getIntArrayFromString(solution.toString());
generateFile(list);
List<Combo> comboUno = new ArrayList<Combo>();
List<Combo> comboDos = new ArrayList<Combo>();
List<Combo> comboTres = new ArrayList<Combo>();
List<Combo> comboCuatro = new ArrayList<Combo>();
List<Combo> comboCinco = new ArrayList<Combo>();
List<Combo> comboSeis = new ArrayList<Combo>();
for (int a = 0; a < list.length; a++) {
switch (list[a]) {
case 1:
for (int b = 0; b < one.length; b++) {
comboUno.add(new Combo(list[a], one[b]));
}
break;
case 2:
for (int b = 0; b < two.length; b++) {
comboDos.add(new Combo(list[a], two[b]));
}
break;
case 3:
for (int b = 0; b < three.length; b++) {
comboTres.add(new Combo(list[a], three[b]));
}
break;
case 4:
for (int b = 0; b < four.length; b++) {
comboCuatro.add(new Combo(list[a], four[b]));
}
break;
case 5:
for (int b = 0; b < five.length; b++) {
comboCinco.add(new Combo(list[a], five[b]));
}
break;
case 6:
for (int b = 0; b < six.length; b++) {
comboSeis.add(new Combo(list[a], six[b]));
}
break;
}
}
}
public static void show(String s) {
for (int i = 0; i < s.length(); i++) {
System.out.print(s.substring(i, i + 1));
if (i != s.length() - 1)
System.out.print("-");
}
}
public static int[] getIntArrayFromString(String s) {
int[] array = new int[203];
for (int i = 0; i < array.length; i++) {
array[i] = Integer.valueOf((s.substring(i, i + 1)));
}
return array;
}
public static void generateFile(int[] array) {
PrintWriter writer;
int cur = -1;
try {
writer = new PrintWriter("combos.txt");
for (int i = 0; i < array.length; i++) {
if (cur + 7 == i) {
cur = i;
writer.println(array[i]);
} else {
writer.print(array[i]);
}
}
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private static class Combo {
int id, c;
public Combo(int id, int c) {
this.id = id;
this.c = c;
}
public int getId() {
return id;
}
public int getC() {
return c;
}
}
}
在他們有一個字母的圖片太 1A = C5,P1,Z2 但我認爲這些字母(C,P,Z)可能在代碼中被忽略。
在此先感謝。
在閱讀了那段文字之後,我仍然不知道你在做什麼。 –