【问题标题】:Create an array of combinations checking an item in it创建一个组合数组,检查其中的项目
【发布时间】:2016-05-08 12:28:10
【问题描述】:

我正在尝试生成一个随机组合列表。每个组合都有一个从 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) 在代码中可以忽略。

提前致谢。

【问题讨论】:

  • 看完那堵文字墙,我还是不知道你在做什么。

标签: java arrays string sorting random


【解决方案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();
        }

    }

}

【讨论】:

    猜你喜欢
    • 2012-10-08
    • 2017-10-13
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 2022-06-15
    • 1970-01-01
    相关资源
    最近更新 更多