创建一个组合数组,检查其中的项目

时间:2016-01-30 21:03:34

标签: java arrays string sorting random

我试图生成随机的组合列表。每个组合具有1到6的数字,以及从a到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

组合ID不同,所以没问题,但组合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 个答案:

答案 0 :(得分: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();
        }

    }

}