问题已经在计算器之前讨论的那样,我专门询问意见或回答关于我的代码和它是否能与不平衡的2D阵列,而无需大修。无法打印某些平衡阵列的末端的原因必定是一些小问题。 更新在底部的Java 2D阵列螺旋/顺时针遍历
基本上我们有由命令行驱动的文本文件中提供的2D阵列。该文件每次尝试都用换行符隔开,如下所示:rows; columns; values(white space delimited)
示例:4; 4; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
输出:1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package spiralprinting;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
*
* @author Paul
*/
public class SpiralPrinting {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO code application logic here
File file = new File(args[0]);
BufferedReader in = new BufferedReader(new FileReader(file));
String line;
while ((line = in.readLine()) != null) {
String[] lineArray = line.split(";");
if (lineArray.length > 0) {//ignore blank line inputs
//Process line of input Here
//Max ,minimum, and current indexes in our matrix.
int maxX = Integer.parseInt(lineArray[0]) - 1;
int maxY = Integer.parseInt(lineArray[1]) - 1;
int minX = 0;
int minY = 0;
int x = 0;
int y = 0;
//Build our matrix
String[] valueArray = lineArray[2].split("\\s");
String[][] matrix = new String [Integer.parseInt(lineArray[0])][Integer.parseInt(lineArray[1])];
int count = 0;
for (int j = 0; j <= maxY; j++){
for (int i = 0; i <= maxX; i++){
matrix[i][j] = (valueArray[count]);
count++;
}
}
StringBuilder printString = new StringBuilder();
//Traverse and print our matrix in a spiral!
while (maxX > minX && maxY > minY){
//Leaving this in and commented so you can see my train of thought.
if (x != maxX){
while (x < maxX){
printString.append(matrix[x][y]).append(" ");
x++;
}maxX--;
}
if (y != maxY){
while (y < maxY){
printString.append(matrix[x][y]).append(" ");
y++;
}maxY--;
}
if (x != minX){
while (x > minX){
printString.append(matrix[x][y]).append(" ");
x--;
}minX++;
}
if (y != minY){
while (y > minY){
printString.append(matrix[x][y]).append(" ");
y--;
}minY++;
}
//One border done (4 passes). Next iteration of while-loop begins.
x = minX;
y = minY;
}//end of our traversal loop
//Print it !
System.out.println(printString.toString().trim());
}
}//end of input line analysis
}
}//end of class
样品输入和电流输出:
4; 4; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ---> 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 好的
3; 3; 1 2 3 4 5 6 7 8 9 ---> 1 2 3 6 9 8 7 4 这无法打印5
3; 4; 1 2 3 4 5 6 7 8 9 10 11 12 ---> 1 2 3 6 9 12 11 10 7 4 ..无法打印5,8末端...
4; 3; 1 2 3 4 5 6 7 8 9 10 11 12 ---> 1 2 3 4 8 12 11 10 9 5 ..不能打印最后2个:6,7“
2; 10; 1 ... ... 20 ---> 1,2,4,6,8 .... goo d
一些快速的修改之后,我的问题似乎是,它不是为一些集打印最后2。我相信这是一个特殊的情况,我要睡觉就可以了:)
任何帮助仍然是赞赏,特别是如果你认为这个问题是大于我目前认为。我困了大脑认为我需要2个特殊情况与我在while循环4个检查一起去......
谢谢=]
感谢您考虑看看。看起来好像我在想着初始化。我编辑了我的帖子以反映当前问题。我当然会花更多的时间进行调试,只要我能解决问题并进行更新,但我仍然希望有人会看到什么是一个大问题。它仍然无法在3乘4阵列上打印最后一个数字'5',并且当我提交给codeeval自动分级器时,它会得到10%:-(。 – spacecadet
@spacecadet如果你有一个3x3的方块并且完成了整个你的minX从0开始,maxX从2开始。快进1个边框循环,你的miX = 1,maxX = 1,x = 1 ...你的while循环会再次运行吗?不,不会,这只是正方形的情况下,只有当它到达中心,因为它已经缩小到1x1的正方形。修正?将这些代码拼凑在一起,如果它的方矩阵是中心数字,那么最好是运气好,上帝的速度。 –
这肯定有点帮助!那里有一个问题,起源于最大等于最小值时。基本上它在阵列平衡时触发,但似乎有奇数个元素。我的解决方法是保留一个附加元素的计数器,并且如果它小于最后的总数,则附加在中值X + 1中值Y + 1上。这很好地解决了我的测试案例。虽然它在自动平地机上只做了一点点改进,从10%上升到了30%。叹息@ _ @ – spacecadet