2016-01-30 33 views
2

我試圖生成一個隨機的組合列表。每個組合都有一個從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; 
     } 

    } 

} 

圖片:http://prntscr.com/9wumdl

在他們有一個字母的圖片太 1A = C5,P1,Z2 但我認爲這些字母(C,P,Z)可能在代碼中被忽略。

在此先感謝。

+1

在閱讀了那段文字之後,我仍然不知道你在做什麼。 –

回答

1
import java.io.*; 
import java.util.*; 
import java.util.stream.Collectors; 
import java.util.stream.IntStream; 

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, 5, 6, 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 String[] LETTERS = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", 
      "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "BB", "CC" }; 

    public static Map<Integer, List<Integer>> buildCombinationMap() { 
     Map<Integer, List<Integer>> combos = new HashMap<Integer, List<Integer>>(); 
     combos.put(1, IntStream.of(one).boxed().collect(Collectors.toList())); 
     combos.put(2, IntStream.of(two).boxed().collect(Collectors.toList())); 
     combos.put(3, IntStream.of(three).boxed().collect(Collectors.toList())); 
     combos.put(4, IntStream.of(four).boxed().collect(Collectors.toList())); 
     combos.put(5, IntStream.of(five).boxed().collect(Collectors.toList())); 
     combos.put(6, IntStream.of(six).boxed().collect(Collectors.toList())); 
     return combos; 
    } 

    public static void main(String[] args) { 

     int[] nums = new int[2000]; 

     for (int i = 0; i < nums.length; i++) { 

      Integer[] arr = new Integer[5]; 
      for (int j = 0; j < arr.length; j++) { 
       arr[j] = j + 2; 
      } 
      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 generatedInput = new StringBuilder(); 

     // Phase 1 
     // Generate a sequence of the numbers 2-6 in a random fashion 
     // There must be three different numbers before repeating the same number 
     int[] nextValidPos = { -1, -1, -1, -1, -1, -1 }; 
     int pos = 0; 
     while (generatedInput.length() < 183) { 
      int nextNumber = Integer.valueOf(numbers.substring(pos, pos + 1)); 
      if (nextValidPos[nextNumber - 1] <= generatedInput.length()) { 
       generatedInput.append(nextNumber); 
       nextValidPos[nextNumber - 1] = generatedInput.length() + 3; 
      } 
      pos++; 
     } 

     // Phase 2 
     // Now the ones are randomly inserted so that a one appear in a seven digit sequence by pattern, 1, 2, 4, 5, 7, 8.. 
     Random r = new Random(); 
     for (int i = 0; i<10; i++) { 
      int oneOffset1 = r.nextInt(7); 
      int oneOffset2 = 7+r.nextInt(7); 
      if ((oneOffset2 - oneOffset1) < 4) 
       oneOffset2 = oneOffset1+4; 
      int baseOffset = i*21; 
      generatedInput.insert(baseOffset+oneOffset1, 1); 
      generatedInput.insert(baseOffset+oneOffset2, 1); 
     } 

     System.out.println("Input string len="+generatedInput.toString().length()); 
     showLines(generatedInput.toString()); 

     String solution = solveCombination(generatedInput.toString()); 
     System.out.println(solution); 

     //generateFile(getIntArrayFromString(generatedInput.toString())); 
     System.out.println("Done"); 

    } 

    public static final int SOLUTION_LEN = 29*7; 

    public static String solveCombination(String keys) { 
     Map<Integer, List<Integer>> comboMap = buildCombinationMap(); 
     int columnPlaceHolder[] = {0, 0, 0, 0, 0, 0}; // Holds the column location; start at 'A' 
     int[] nextValidPos = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; // 13 slots needed; max value=13 
     StringBuilder solution = new StringBuilder(); 
     int pos = 0; 
     int keySize = 0; 
     while (pos < SOLUTION_LEN) { 
      int nextNumber = Integer.valueOf(keys.substring(pos, pos + 1)); 
      //System.out.println("Pos=" + pos + "; Working on: " + nextNumber); 
      boolean found = false; 
      // Search for the next available combo 
      while (!found) { 
       int i = comboMap.get(nextNumber).get(columnPlaceHolder[nextNumber-1]); 
       //System.out.println("Trying=" + i); 
       if (nextValidPos[i - 1] <= pos) { 
        solution.append(nextNumber); 
        solution.append(LETTERS[columnPlaceHolder[nextNumber-1]]); 
        if (pos < SOLUTION_LEN - 1) 
         solution.append("-"); 
        nextValidPos[i - 1] = pos + 4; 
        //System.out.println("SOLUTION=" + solution.toString()); 
        found = true; 
        keySize++; 
       } 
       // Move to next position, and wrap back to A if you reach the end 
       columnPlaceHolder[nextNumber-1]++; 
       if (columnPlaceHolder[nextNumber-1] == comboMap.get(nextNumber).size()) 
        columnPlaceHolder[nextNumber-1] = 0; 
       } 
      pos++; 
     } 
     System.out.println("Key size="+keySize); 
     return solution.toString(); 
    } 

    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 void showLines(String s) { 
     for (int i = 0; i < s.length()/7; i++) { 
      System.out.println(s.substring(i*7, i*7 + 7)); 
     } 
    } 

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

    } 

} 
相關問題