gpt4 book ai didi

java - 如何在 java 中制作 ulam 螺旋?

转载 作者:行者123 更新时间:2023-11-29 07:57:48 26 4
gpt4 key购买 nike

<分区>

所以我今天花了几个小时写出逻辑,并将其转化为代码,但我完全卡在了这一点上,我不知道该怎么做。我现在才用 Java 编程几个月,所以整个“逻辑”思维方式还不完全存在。谁能帮我想清楚如何在 Java 中创建 ulam 螺旋的逻辑?

import java.util.Arrays;

public class GridMaker {
private static int gridRow = 5; // R = length
private static int gridCol = 5; // C = height
private static int[][] grid = new int[gridRow][gridCol];
private static int totalSteps = (gridRow * gridCol); // total blocks on the grid
private static int location = 1; // location refers to the number in the box, ie. 1, 2, 3, etc.
private static int rowLength = 1;

public static void main(String[] args) {
grid[Calc.findArrayCenter(gridRow)][Calc.findArrayCenter(gridRow)] = 1;
rowBrowser();
colBrowser();

for (int r = 0; r < gridRow; r++){
for (int c = 0; c < gridCol; c++){
System.out.print(grid[r][c] + " ");
}
System.out.println("");
}
}

public static void rowBrowser() {
int rowCount = 1;
int x = 1;
int stepsInvolved = 2;

if (x < stepsInvolved) {
if (Calc.isOdd(rowCount) == true) {
grid[Calc.findArrayCenter(gridRow)][Calc.findArrayCenter(gridCol) + x] = location + 1;
stepsInvolved++;
}
}
location++;
x++;
}

private static void colBrowser() {

}
}

    public class Calc {
public static int findArrayCenter(int center) {
int fcenter = 0;
if (center % 2 != 0)
fcenter = (int) ((center / 2));
else
fcenter = (center / 2);
return fcenter;
}

public static boolean isOdd(int num) {
boolean result = true;
if (num % 2 == 0)
result = false; // false = even, true = odd
return result;
}
}

此时,我需要做什么才能完成 ulam 螺旋线的创建?我现在正在做的是让数组跟踪一个位置,在一行中运行每个步骤,然后下拉并运行一列中的步骤,之后将每个计数器加 1 并继续。帮助?对于糟糕的格式感到抱歉,这个网站在粘贴代码时并没有太大帮助......:|

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