-2

How to make combination alphabet looping in Java like example output like this

A1 B1 C1 D1
A1 B1 C1 D2
A1 B1 C1 D3
A1 B1 C2 D1
A1 B1 C2 D2
A1 B1 C2 D3
A1 B2 C1 D1
A1 B2 C1 D2
A1 B2 C1 D3
A1 B2 C2 D1
A1 B2 C2 D2
A1 B2 C2 D3
A1 B3 C1 D1
A1 B3 C1 D2
A1 B3 C1 D3
A1 B3 C2 D1
A1 B3 C2 D2
A2 B3 C2 D3
A2 B1 C1 D1
A2 B1 C1 D2
A2 B1 C1 D3
A2 B1 C2 D1
A2 B1 C2 D2
A2 B1 C2 D3
A2 B2 C1 D1
A2 B2 C1 D2
A2 B2 C1 D3
A2 B2 C2 D1
A2 B2 C2 D2
A2 B2 C2 D3
A2 B3 C1 D1
A2 B3 C1 D2
A2 B3 C1 D3
A2 B3 C2 D1
A2 B3 C2 D2
A2 B3 C2 D3

For this combination like matrix (36 x 4). Column 1 is 18 for A1, 18 for A2. Column 2 is 6 for B1, 6 for B2, 6 for B3. Column 3 is 3 for C1, 3 for C2. Column 4 is 1 for D1, 1 for D2, 1 for D3. Every column have 36 data. Thank's.

4

2 回答 2

1

人们可能会因为我赠送一条鱼而攻击我。随它吧。

public class Scratch1
{
    private static class Nibble {
        private String[] symbols;
        private int      numSymbols;
        private int      numRepeats;

        public Nibble(int numRepeats, String... symbols) {
            this.symbols = symbols;
            this.numSymbols = symbols.length;
            this.numRepeats = numRepeats;
        }

        public String toString(int i) {
            return symbols[(i / numRepeats) % numSymbols];
        }
    }

    private static class Column {
        private Nibble firstNibble;
        private Nibble secondNibble;

        public Column(Nibble firstNibble, Nibble secondNibble) {
            this.firstNibble = firstNibble;
            this.secondNibble = secondNibble;
        }

        public String toString(int i) {
            return firstNibble.toString(i) + secondNibble.toString(i);
        }
    }

    public static void main(String[] args)
    {
        Column columns[] = {
            new Column( new Nibble(18, "A"), new Nibble(18, "1", "2")),
            new Column( new Nibble(18, "B"), new Nibble( 6, "1", "2", "3")),
            new Column( new Nibble( 6, "C"), new Nibble( 3, "1", "2")),
            new Column( new Nibble( 3, "D"), new Nibble( 1, "1", "2", "3"))
        };

        for (int i = 0 ; i < 36 ; i++) {
            for (int colNum = 0 ; colNum < columns.length ; colNum++) {
                if (colNum > 0) System.out.print(" ");
                System.out.print(columns[colNum].toString(i));
            }
            System.out.println();
        }
    }
}
于 2013-06-14T04:24:12.100 回答
1

我认为模式是:主数组有 4 个元素(A B C D),

  • 我们从 A 循环到 D
  • D 有 3 个元素(从 D1 到 D3)
  • C有2个元素(从C1到C2)
  • B 有 3 个元素(从 B1 到 B3)
  • A 有 2 个元素(从 A1 到 A2)

伪代码:

String[2] A = {"A1","A2"};
String[3] B = {"B1","B2","B3"};
String[2] C = {"C1","C2"};
String[3] D = {"D1","D2","D3"};
for(int i = 0; i < A.length;i++){
   for(int j = 0;j < B.length;j++){
     for(int t = 0; t < C.length;t++){
        for(int e = 0; e < D.length;e++){
            System.out.printfln(A[i] + " " + B[j] + " " + C[t] + " " + D[e]);
        }
     }
   }
}
于 2013-06-14T03:40:42.037 回答