我想要做的是打印出一个从末尾递增的递增集(参见下面的示例)。
我拥有的代码采用一组运算符并逐个更改它们,从最后开始并向后工作。这是我所拥有的(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
等等,对于我想要产生的突变数量。我需要如何修改算法或者有什么(我确定有)更简单的方法来实现这一点?我什至不确定我要问的名称,所以请根据需要标记。