1

私がやろうとしているのは、最後からインクリメントするインクリメント セットを出力することです (以下の例を参照)。

私が持っているコードは一連の演算子を取り、最後から逆方向に 1 つずつ変更します。これが私が持っているものです(mutは突然変異です):

public static void main(String[] args) {
    String[] set = {"*", "*", "*"};
    int numOfMuts = 6;

    int currMutIndex = set.length - 1;
    String currOp = set[currMutIndex];
    String nextMut = currOp;

    for (int i = 1; i <= numOfMuts; i++) {
        nextMut = shiftOperator(nextMut);
        if (nextMut.equals(currOp)) {
            set[currMutIndex] = currOp;

            if ((currMutIndex--) == -1) {
                break;
            }

            currOp = set[currMutIndex];
            nextMut = shiftOperator(currOp);
        }
        set[currMutIndex] = nextMut;

        //print out the set
        printSet(set);
    }
}

/*
    This method shifts the operator to the next in the set of
    [*, +, -, /]. This is the order of the ASCII operator precedence.
*/
public static String shiftOperator(String operator) {
    if (operator.equals("*")) {
        return "+";
    } else if (operator.equals("+")) {
        return "-";
    } else if (operator.equals("-")) {
        return "/";
    } else { //operator is "/"
        return "*";
    }
}

それは私に与えます:

*, *, +
*, *, -
*, *, /
*, +, *
*, -, *
*, /, *

しかし、私が欲しいのは:

*, *, +
*, *, -
*, *, /
*, +, *
*, +, +
*, +, -

数字を使用して、さらに簡単な言葉で問題を説明するには、次のようにします。

1, 1, 1       1, 3, 1
1, 1, 2       1, 3, 2
1, 1, 3       1, 3, 3
1, 1, 4       1, 3, 4
1, 2, 1       1, 4, 1
1, 2, 2       1, 4, 2
1, 2, 3       1, 4, 3
1, 2, 4       1, 4, 4
1, 3, 1       2, 1, 1
1, 3, 2       2, 1, 2

生成したい突然変異の数など。アルゴリズムをどのように変更する必要がありますか、またはこれを達成するためのより簡単な方法は何ですか? 質問の名前すらわからないので、必要に応じてタグ付けしてください。

4

1 に答える 1

1

基本的に、あなたがやろうとしていることは、第四紀で数えることです。バイナリが数値としてどのように構造化されるかによく似ています。

2^(n-1) 2^(n-2) ... 2^1 2^0

上に数えると、0、1、10、11、100 などの結果になります。

四次系は、次を使用して同じ方法でカウントします。

4^(n-1) 4^(n-2) ... 4^1 4^0

その結果は、0、1、2、3、10、11、12、13、20 などになります。任意の数システムで同じことができます。あなたが求めていることを行うコードは次のとおりです。

    String set[] = new String[3];
    String countSymbols[] = {"*", "+","-","/"}

    /* You can set to however far you want to count here, but in your code, 
      the limit would be (4^3)-1 = 3*(4^2)+3(4^1)+3*(4^0) = 63. We get this
      because there's 4 symbols and 3 digits. */

    for (int i = 0 ; i < 64 ; i++) {
       /* Since you're trying to print them in increasing order, we'll have to set
          the values in reverse order as well. */
       // 4^0 = 1
       set[2] = countSymbols[i%4];

       // 4^1 = 4
       set[1] = countSymbols[(i/4)%4]

       // 4^2 = 16
       set[0] = countSymbols[(i/16)%4]

       printSet(set);
    }

お役に立てれば。

于 2013-10-04T02:30:09.217 に答える