Created
June 23, 2015 09:02
-
-
Save wicksome/1428d23ad0f1b44169d1 to your computer and use it in GitHub Desktop.
java 달팽이 만들기
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Test1; | |
| import java.util.Scanner; | |
| import java.util.StringTokenizer; | |
| public class CodingTest { | |
| public static void main(String[] args) { | |
| Scanner s = new Scanner(System.in); | |
| StringTokenizer stk; | |
| int row = -1, col = -1; | |
| while (true) { | |
| stk = new StringTokenizer(s.nextLine(), " "); | |
| if (stk.countTokens() == 2) { | |
| row = Integer.parseInt(stk.nextElement().toString()); | |
| col = Integer.parseInt(stk.nextElement().toString()); | |
| break; | |
| } else { | |
| System.out.println("다시 입력해주세요.(ex. 6 6)"); | |
| } | |
| } | |
| new CodingTest().printFunction(row, col); | |
| } | |
| void printFunction(int r, int c) { | |
| int[][] arr = new int[r][c]; | |
| int rowIdx = 0; | |
| int colIdx = 0; | |
| int num = 0; | |
| int rowMin = 0, rowMax = r - 1; | |
| int colMin = 0, colMax = c - 1; | |
| boolean goLeft = true; | |
| boolean goDown = false; | |
| boolean goRight = false; | |
| boolean goUp = false; | |
| while (num != r * c) { | |
| if (goLeft) { | |
| if (colIdx > colMax) { | |
| rowMin++; | |
| colIdx--; | |
| rowIdx++; | |
| goLeft = false; | |
| goDown = true; | |
| continue; | |
| } | |
| arr[rowIdx][colIdx++] = num; | |
| } else if (goDown) { | |
| if (rowIdx > rowMax) { | |
| colMax--; | |
| colIdx--; | |
| rowIdx--; | |
| goDown = false; | |
| goRight = true; | |
| continue; | |
| } | |
| arr[rowIdx++][colIdx] = num; | |
| } else if (goRight) { | |
| if (colIdx < colMin) { | |
| rowMax--; | |
| colIdx++; | |
| rowIdx--; | |
| goRight = false; | |
| goUp = true; | |
| continue; | |
| } | |
| arr[rowIdx][colIdx--] = num; | |
| } else if (goUp) { | |
| if (rowIdx < rowMin) { | |
| colMin++; | |
| rowIdx++; | |
| colIdx++; | |
| goUp = false; | |
| goLeft = true; | |
| continue; | |
| } | |
| arr[rowIdx--][colIdx] = num; | |
| } | |
| num++; | |
| } | |
| // output | |
| for (int i = 0; i < arr.length; i++) { | |
| for (int j = 0; j < arr[i].length; j++) { | |
| System.out.printf("%2d ", arr[i][j]); | |
| } | |
| System.out.println(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment