티스토리 뷰

1641 : 숫자삼각형

jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=914&sca=2020

 

JUNGOL

 

www.jungol.co.kr

import java.util.Scanner;

// 210126
// 1641 : 숫자삼각형
// 18MB, 386ms, Success

public class J1641 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		int m = sc.nextInt();

		if (n % 2 == 0 || n > 100) {
			System.out.println("INPUT ERROR!");
		} else {
			if (m == 1) {
				int num = 1;
				for (int i = 0; i < n; i++) {
					// 짝수줄
					if (i % 2 == 0) {
						for (int j = 0; j <= i; j++) {
							System.out.print(num++ + " ");
						}
						System.out.println();
					}
					// 홀수줄
					else {
						int max = num + i;
						for (int j = 0; j <= i; j++) {
							System.out.print(max-- + " ");
							num++;
						}
						System.out.println();
					}
				}
			} // if
			else if (m == 2) {
				int num = 0;
				int cnt = n * 2 - 1;
				for (int i = 0; i < n; i++) {
					// 공백 출력
					for (int j = 0; j < i; j++) {
						System.out.print("  ");
					}
					// 숫자 출력
					for (int j = 0; j < cnt; j++) {
						System.out.print(num + " ");
					}
					num++;
					cnt -= 2;
					System.out.println();

				}
			} // else if
			else if (m == 3) {
				int mid = n / 2;
				for (int i = 0; i < n; i++) {
					if (i <= mid) {
						for (int j = 0; j <= i; j++) {
							System.out.print((j + 1) + " ");
						}
					} else {
						for (int j = 0; j < mid; j++) {
							System.out.print((j + 1) + " ");
						}
						mid--;
					}
					System.out.println();
				}
			} // else if
			else {
				System.out.println("INPUT ERROR!");
			}
		}

		sc.close();
	} // main

}

'코딩테스트 > 정올' 카테고리의 다른 글

[JUNGOL] 1681. 해밀턴 순환회로  (0) 2021.03.22
[JUNGOL] 1146 : 선택정렬  (0) 2021.01.27
[JUNGOL] 1697 : 큐(queue)  (0) 2021.01.27
[JUNGOL] 1102 : 스택 (stack)  (0) 2021.01.27
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함