루빅스 큐브를 인쇄하십시오
자연 숫자 n (2≤n≤9)을 입력하고 다음 큐브 행렬을 출력해야합니다. 즉, 측면 길이는 n*n이고 요소는 1에서 n*n까지, 1은 왼쪽 상단에 있으며 각 요소는 시계 방향으로 배치됩니다. n = 3 일 때 :
1 2 3 8 9 4 7 6 5
[입력 양식] 표준 입력에서 정수 N을 읽습니다.
[출력 양식] 결과를 표준 출력에 인쇄합니다. 요구 사항을 충족하는 사각형 매트릭스는 출력이며, 각 숫자는 폭이 5자를 차지하고 오른쪽에 정렬되며, 각 라인의 끝 부분에 캐리지 리턴 문자가 출력됩니다.
【입력 샘플 sample 4
【출력 샘플 sample
1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7
성취하다:
패키지 cn.dfeng; import java.util.arrays; java.util.scanner import; 공개 클래스 미로 {enum Direction {up, 아래, 오른쪽, 왼쪽; } public int [] [] buidmaze (int n) {int [] [] maze = new int [n] [n]; for (int [] a : maze) {arrays.fill (a, 0); } int col = 0; int row = 0; int 카운터 = 1; 방향 d = 방향. while (true) {if (maze [row] [col] == 0) {maze [row] [col] = counter ++; 스위치 (d) {case 오른쪽 : if (col + 1 <n && maze [row] [col + 1] == 0) {col ++; } else {d = Direction.Down; 행 ++; } 부서지다; 케이스 다운 : if (row + 1 <n && maze [row + 1] [col] == 0) {row ++; } else {d = direction.left; col-; } 부서지다; 왼쪽 사례 : if (col -1> = 0 && maze [row] [col -1] == 0) {col-; } else {d = direction.up; 열 --; } 부서지다; 기본값 : if (행 -1> = 0 && maze [row -1] [col] == 0) {row-; } else {d = Direction.right; col ++; } 부서지다; }} else {break; }} 리턴 미로; } public void printmaze (int [] [] maze) {for (int [] row : maze) {for (int i : row) {system.out.printf ( "%3d", i); } system.out.println (); }} / ** * @param args * / public static void main (String [] args) {스캐너 sc = new 스캐너 (System.in); System.out.println ( "미로의 크기를 입력하십시오 :"); int size = sc.nextint (); 미로 미로 = 새로운 미로 (); int [] [] m = maze.buidmaze (size); Maze.printmaze (M); }}다이아몬드 그래픽 인쇄
다이아몬드 사진의 효과는 아마도 다음과 같습니다.
아래 코드를 살펴 보겠습니다
패키지 cn.dfeng; / ***이 클래스는 ** @author dfeng*/ public class 서랍 {/ *** 인쇄 다이아몬드 그래픽* @param n 다이아몬드 크기*/ public void printdiamond (int n) {system.out.println (); int i = 0; 부울 플래그 = 참; while (i> = 0) {if (i <n) {for (int j = 0; j <n -i; j ++) {system.out.print ( ""); } for (int j = n -i; j <= n + i; j + = 2) {system.out.print ( "*"); } system.out.println (); } if (i == n) {flag = false; 나--; } if (flag) {i ++; } else {i-; }}}}