布尔数组和尝试每种组合的简单方法
Boolean array and a simple way to try every combination
所以目前我正在为我上一篇 comp sci 论文写一些代码,它要求你在 system.in 上输入一些红色,处理它,第一行总是一组数字,最多 25 个数字.接下来是单个 N 或 L,以及另一个 int,它是目标。使用此输入,您必须找到使用 int 值创建目标的正确操作集(+ 和 *)。
我正在使用一个布尔数组来跟踪我在非常检查中使用的操作数但是我不确定如何通过尝试每组不同的操作数来"brute force"解决方案,我有代码来检查每组但是我不确定是否有一种简单易行的方法来更改数组,例如 [0,0,0,0](0 为假)到 [0,0,0,1],[0, 0,1,0], [0,0,1,1] 等?
我确定我忽略了一个非常简单的方法,但对于我的生活,我不确定它是什么 atm。
static boolean evlN(int[] input, boolean[]ops, int aim){
boolean run = true, found = false;
int[] used = new int[input.length];
int runs = 0 ,ans = 0;
while(!found && runs < (1 << ops.length)){
//finding all multiplys and doing them first
search:
for(int x = 0; x < ops.length; x++){
if(!ops[x]){
used[x] = input[x] * input[x+1];
//need to stop working out and change the ops
if(used[x] > aim){
run = false;
break;
}
}
}
//once multiplys have been done need to do all the adds
if(run){
for(int x = 0; x < ops.length; x++){
if(ops[x]){
if(used[x] != 0) ans += used[x] + input[x+1];
else if(used[x+1] != 0) ans += input[x] + used[x];
}
if(ans > aim) break;
}
}
if(ans == aim) found = true;
used = new int[input.length];
ans= 0;
runs++;
run = !run;
}
if(found) return true;
else return false;
}
这就是我用来计算每组操作数和数字的方法,我只是想更改布尔数组以暴力破解答案
您的输入组合集看起来像一个二进制整数(称之为 N
)。您可以通过递增 N
.
来完成不同的组合
有一种相当通用的机制可用于递增一组组合,就像您对整数中的数字所做的那样。我将使用界面对其进行演示,以便您了解它的一般应用方式。
public interface UnitValue {
boolean isLast();
UnitValue next();
}
public class <T extends UnitValue> MultiUnitValue {
private final int size;
private final T first;
private final T[] units;
private boolean complete = false;
public MultiUnitValue(int size, T first) {
this.size = size;
this.first = first;
this.units = new T[size];
for (int i = 0; i < size; i++)
units[i] = first;
}
public void next() {
if (!complete) {
int i = 0;
while (units[i].isLast())
units[i++] = first;
units[i].next();
complete = i == size - 1 && units[i].isLast();
}
}
}
为了清楚起见,我省略了吸气剂,但它们应该是显而易见的。
对于布尔值的 non-generic 解决方案,它看起来像:
boolean[] values = new boolean[size];
int i = 0;
while (values[i])
values[i++] = false;
values[i] = true;
一个非常相似的解决方案适用于字符、数字、枚举和任何其他符合相同模式的东西。
所以目前我正在为我上一篇 comp sci 论文写一些代码,它要求你在 system.in 上输入一些红色,处理它,第一行总是一组数字,最多 25 个数字.接下来是单个 N 或 L,以及另一个 int,它是目标。使用此输入,您必须找到使用 int 值创建目标的正确操作集(+ 和 *)。
我正在使用一个布尔数组来跟踪我在非常检查中使用的操作数但是我不确定如何通过尝试每组不同的操作数来"brute force"解决方案,我有代码来检查每组但是我不确定是否有一种简单易行的方法来更改数组,例如 [0,0,0,0](0 为假)到 [0,0,0,1],[0, 0,1,0], [0,0,1,1] 等?
我确定我忽略了一个非常简单的方法,但对于我的生活,我不确定它是什么 atm。
static boolean evlN(int[] input, boolean[]ops, int aim){
boolean run = true, found = false;
int[] used = new int[input.length];
int runs = 0 ,ans = 0;
while(!found && runs < (1 << ops.length)){
//finding all multiplys and doing them first
search:
for(int x = 0; x < ops.length; x++){
if(!ops[x]){
used[x] = input[x] * input[x+1];
//need to stop working out and change the ops
if(used[x] > aim){
run = false;
break;
}
}
}
//once multiplys have been done need to do all the adds
if(run){
for(int x = 0; x < ops.length; x++){
if(ops[x]){
if(used[x] != 0) ans += used[x] + input[x+1];
else if(used[x+1] != 0) ans += input[x] + used[x];
}
if(ans > aim) break;
}
}
if(ans == aim) found = true;
used = new int[input.length];
ans= 0;
runs++;
run = !run;
}
if(found) return true;
else return false;
}
这就是我用来计算每组操作数和数字的方法,我只是想更改布尔数组以暴力破解答案
您的输入组合集看起来像一个二进制整数(称之为 N
)。您可以通过递增 N
.
有一种相当通用的机制可用于递增一组组合,就像您对整数中的数字所做的那样。我将使用界面对其进行演示,以便您了解它的一般应用方式。
public interface UnitValue {
boolean isLast();
UnitValue next();
}
public class <T extends UnitValue> MultiUnitValue {
private final int size;
private final T first;
private final T[] units;
private boolean complete = false;
public MultiUnitValue(int size, T first) {
this.size = size;
this.first = first;
this.units = new T[size];
for (int i = 0; i < size; i++)
units[i] = first;
}
public void next() {
if (!complete) {
int i = 0;
while (units[i].isLast())
units[i++] = first;
units[i].next();
complete = i == size - 1 && units[i].isLast();
}
}
}
为了清楚起见,我省略了吸气剂,但它们应该是显而易见的。
对于布尔值的 non-generic 解决方案,它看起来像:
boolean[] values = new boolean[size];
int i = 0;
while (values[i])
values[i++] = false;
values[i] = true;
一个非常相似的解决方案适用于字符、数字、枚举和任何其他符合相同模式的东西。