본문 바로가기

프로그래밍/자바(JAVA)

[자바/JAVA] 자바 골뱅이, 역골뱅이 만들기.

자바 골뱅이, 역골뱅이 만들기.

1. 골뱅이 & 역골뱅이

import java.io.*;

class At{
 public static void main(String[] args) throws IOException{
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("숫자입력 : ");
  int num = Integer.parseInt(in.readLine());

  At_1 a1 = new At_1(num); //골뱅이
  At_2 a2 = new At_2(num); //역골뱅이
 }
}


class At_1{
 At_1(int num){
  int at[][] = new int[num][num];
  int count;
  int i=0, j=0;
  int temp_1=0, temp_2=num-1;

  for (count=0;count<num*num ;count++ ){
   at[i][j] = count+1;
   if (j == temp_2 && i != temp_2){
    i++;
   }else if (i == temp_2 && j != temp_1){
    j--;
   }else if (j == temp_1 && i != temp_1 && i != temp_1+1){
    i--;
   }else if (j == temp_1 && i == temp_1+1){
    temp_1++;
    temp_2--;
    j++;
   }else{
    j++;
   }
  }

  for (i=0;i<num ;i++ ){
   for (j=0;j<num ;j++ ){
    System.out.print(at[i][j]+"\t");
   }
   System.out.println();
  }
  System.out.println();
 }
}


class At_2{
 At_2(int num){
  int at[][] = new int[num][num];
  int count;
  int i=0, j=0;
  int temp_1=0, temp_2=num-1;

  for (count=num*num;count>0 ;count-- ){
   at[i][j] = count;
   if (j == temp_2 && i != temp_2){
    i++;
   }else if (i == temp_2 && j != temp_1){
    j--;
   }else if (j == temp_1 && i != temp_1 && i != temp_1+1){
    i--;
   }else if (j == temp_1 && i == temp_1+1){
    temp_1++;
    temp_2--;
    j++;
   }else{
    j++;
   }
  }

  for (i=0;i<num ;i++ ){
   for (j=0;j<num ;j++ ){
    System.out.print(at[i][j]+"\t");
   }
   System.out.println();
  }
  System.out.println();
 }
}