Created
July 26, 2016 13:53
-
-
Save developer-sdk/dafd72c9d82e39a23be8a1f1c10f3877 to your computer and use it in GitHub Desktop.
1495, 대각선 지그재그
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 sdk.algo; | |
| import java.util.Scanner; | |
| /** | |
| * 1495, 대각선 지그재그 | |
| * | |
| * @author whitebeard | |
| * | |
| */ | |
| public class Problem1495 { | |
| public static void main(String[] args) { | |
| Scanner in = new Scanner(System.in); | |
| int n = in.nextInt(); | |
| in.close(); | |
| // int n = 5; | |
| int[][] array = new int[n][n]; | |
| // 배열에 입력할 증가값 | |
| int count = 1; | |
| // 배열의 현재 위치 | |
| int currentX = 0; | |
| int currentY = 0; | |
| // 0, 0 위치를 1로 초기화 | |
| array[currentX][currentY] = count++; | |
| while(count <= n*n) { | |
| // x가 n보다 작은 동안은 x 증가, 이후에는 y 증가 | |
| if(currentX + 1 < n) { | |
| currentX++; | |
| } else { | |
| currentY++; | |
| } | |
| array[currentX][currentY] = count++; | |
| // 우상향으로 이동 | |
| while(currentX - 1 > -1 && currentY + 1 < n) { | |
| array[--currentX][++currentY] = count++; | |
| } | |
| // y가 n 보다 작은 동안은 y 증가, 이후에는 x증가 | |
| if(currentY + 1 < n) { | |
| currentY++; | |
| } else { | |
| currentX++; | |
| } | |
| array[currentX][currentY] = count++; | |
| // 좌하향으로 이동 | |
| while(currentY - 1 > -1 && currentX + 1 < n) { | |
| array[++currentX][--currentY] = count++; | |
| } | |
| } | |
| // 결과 출력 | |
| for(int[] xx : array) { | |
| for(int nn : xx) { | |
| System.out.printf("%d ", nn); | |
| } | |
| System.out.println(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment