私がやろうとしているのは、最後からインクリメントするインクリメント セットを出力することです (以下の例を参照)。
私が持っているコードは一連の演算子を取り、最後から逆方向に 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
生成したい突然変異の数など。アルゴリズムをどのように変更する必要がありますか、またはこれを達成するためのより簡単な方法は何ですか? 質問の名前すらわからないので、必要に応じてタグ付けしてください。