問題已經在計算器之前討論的那樣,我專門詢問意見或回答關於我的代碼和它是否能與不平衡的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