숫자는 원하는 깊이 만큼 출력 해줍니다.
오른쪽 부터 왼쪽으로 계단 식으로 출력 합니다.
예를 들어 출력형식은 깊이가 4 라면

               1
         3    2
     6   5    4
10  9   8    7

같은 형태로 출력이 됩니다.

public class NumPrint2 {
 public static void main(String[] args) {
  numPrint(13);
 }
 
 static void numPrint(final int MAXDEPTH) {
  int sum = 0;
  for (int i = 1 ; i < MAXDEPTH+1 ; i++) {
   sum = sum + i;
   int loop = i;
   int temp = sum+1;
   
   for (int j = i + 1 ; 0 < (MAXDEPTH+1)-j ; j++)
    System.out.print("   ");
   
   for (loop = i ; 0 < loop ; loop--)
    System.out.printf("%3d", --temp);
   
   System.out.println();
  }
 }

}

Posted by 마라스
l
어떻게 보면 단순하게 구현 할 수 도 있는 것을 너무 골머리 싸맨듯 합니다.
비슷한 문제가 전자계산기 기사 시험에도 나오는듯 하는데 그것은 배열을
이용하여 하는것 같던데 이것은 순수하게 출력 구문으로만 구성 된 것입니다.
혹시라도 이런 문제에 봉착하신분들께 도움이 될지 도 몰라서 게시 해 봅니다.
아래의 예 처럼 출력 됩니다.
  1  2  3  4  5
     6  7  8
        9
    10 11 12
13 14 15 16 17

public class NumPrint3 { // 모래시계 처럼 누적된 숫자 출력하는 클래스
 public static void main(String[] args) {
  numPrint(7);
 }
 
 static void numPrint(final int MAXDEPTH) {
  int sum = 1;
  for (int i = 0 ; i < MAXDEPTH ; i++) {
   for (int j = 0 ; j < i ; j++)
    System.out.print("   ");
   
   for (int j = 0 ; j < (MAXDEPTH+(MAXDEPTH-1))-(2*i) ; j++) {
    System.out.printf("%3d", sum);
    sum++;
   }
   
   for (int j = 0 ; j < i ; j++)
    System.out.print("   ");
   
   System.out.println();
  }
 
  for (int i = MAXDEPTH-1 ; i > 0 ; i--) {
   for (int j = 0 ; j < i-1 ; j++)
    System.out.print("   ");
   
   for (int j = (MAXDEPTH+(MAXDEPTH-1)) ; j > (i-1)*2 ; j--) {
    System.out.printf("%3d", sum);
    sum++;
   }
   
   for (int j = 0 ; j < i ; j++)
    System.out.print("   ");
   
   System.out.println();
  }
 }
}
Posted by 마라스
l

여기 오신 모든 분들께 축복이 가득하길!

ps. 2주간을 티스토리 블로그를 개설하기 위해 잠복하였으나 계속되는 실패에 좌절하고
     있던차 정말 좋은 기회에 Shine_ing(http://shine-ing.tistory.com)님이 보내주신
     초대장으로 개설하게 되었습니다. 진심으로 감사드립니다.

Posted by 마라스
l