gpt4 book ai didi

java - 将 int 转换为 int 数组

转载 作者:行者123 更新时间:2023-12-02 11:56:58 27 4
gpt4 key购买 nike

现在我有另一个问题。我需要制作一个 CNF 公式并使用 3d 数组的整数图。现在我尝试使用此任务,不同之处在于插入 3d 数组,而不是 2d。

我用列做了它(其中有 2 颗星(**))并且它有效。

使用 block (您可以在其中看到 ********* 星星), eclipse 显示“无法将 int 转换为 []int”

为什么这么写?在“它没有的列”中。

我希望我说得清楚。

public static void encode(int sqrtN, int[][] hints, int[][][] map) {
int[] vars = new int [sqrtN*sqrtN];
int nVars = sqrtN*sqrtN*sqrtN*sqrtN*sqrtN*sqrtN;


** //each cell in the columns gets a different value.**
for(int i=0; i<sqrtN*sqrtN; i=i+1){
for(int j=0; j<sqrtN*sqrtN; j=j+1) {
for(int k=0; k<sqrtN*sqrtN; k=k+1)
vars[k] = columns(map)[i][k][j];

SATSolver.addClauses(exactlyOne(vars));
}
}

*****//each cell in the blocks gets a different value.******
for(int i=0; i<sqrtN*sqrtN; i=i+1){
for(int j=0; j<sqrtN*sqrtN; j=j+1) {
for(int k=0; k<sqrtN*sqrtN; k=k+1)
vars[k] = blocks(map,sqrtN)[i][k][j];

SATSolver.addClauses(exactlyOne(vars));
}
}
//hints.
int [] hintsSATS = new int[1];
for (int i = 0; i < hints.length; i = i + 1) {
hintsSATS[0] = varName(hints[i][0],hints[i][1],hints[i][2]-1,sqrtN*sqrtN);
SATSolver.addClause(hintsSATS);
}

}

//A function to trunsfer to columns.
public static int[][][] columns (int[][][] matrix) {
int[][][] columns = new int[matrix.length][matrix.length][matrix.length];
for(int j=0; j<matrix.length; j= j+1){
for(int i=0; i<matrix.length; i= i+1){
columns[j][i] = matrix[i][j];
}
}
return columns;
}

************ //A function to trunsfer to blocks.**********
public static int[][][] blocks (int[][][] matrix, int sqrtN) {
int[][][] blocks = matrix;
for (int i = 0; i<matrix.length; i= i+1) {
for (int j = 0; j<matrix.length; j= j+1) {
blocks[i][j] = j%sqrtN+ i%sqrtN*sqrtN + 1;
}
}
return blocks;
}




//The name of the variable.
public static int varName(int i, int j, int k, int n) {
int varName = n*n*i+n*j+k+1;
return varName;
}
//functions for the CNF. Excactly one.

public static int[][] atLeastOne(int[] vars) {
int [][] atLeastOne = new int [1][vars.length];
atLeastOne[0] = vars;
return atLeastOne;
}

public static int[][] atMostOne(int[] vars) {
int [][]atMostOne = new int[AmountOfArrays(vars.length)][2];
int k = 0;
for(int i= 0;i<vars.length;i= i+1){
for( int j=i+1;j<vars.length;j= j+1){
atMostOne[k][0] = -vars[i];
atMostOne[k][1] = -vars[j];
k=k+1;
}
}
return atMostOne;
}

public static int AmountOfArrays(int n){
int counter = 0;
for(int i= 0;i<n-1;i= i+1){
for( int j=i+1;j<n;j= j+1){
counter= counter+1;
}
}
return counter;
}

public static int[][] exactlyOne(int[] vars) {
int [][] exactlyOne = new int [AmountOfArrays(vars.length)+1][vars.length];
int [][]atLeastOne = new int [1][vars.length];
atLeastOne = atLeastOne(vars);
exactlyOne[0] = atLeastOne[0];
for(int i=1; i<exactlyOne.length; i= i+1){
int [][]atMostOne = atMostOne(vars);
exactlyOne[i] = atMostOne[i-1];
}
return exactlyOne;
}

最佳答案

问题就在这里blocks[i][j] = j%sqrtN+ i%sqrtN*sqrtN + 1;blocks[i][j] 需要一个数组,但 j%sqrtN+ i%sqrtN*sqrtN + 1; 返回一个 int。

关于java - 将 int 转换为 int 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47527383/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com