내용

글번호 120
작성자 heojk
작성일 2017-01-03 12:28:17
제목 코딩시험 - Task 2
내용 public class Task2 { public static void main(String[] args) { System.out.println(solution(1, 1));//2 System.out.println(solution(1, 0));//3 System.out.println(solution(-1, 2));//9 System.out.println(solution(2, 2));//12 } // - - / +1 +1 / -1 -1 -1 -1 / +1 +1 +1 +1 +1 +1 //(0,0) (0,1) (1,1) (1,0) (1,-1) (0,-1) (-1,-1) (-1,0) (-1,1) (-1,2) (0,2) (1,2) (2,2) // -1 -1 -1 -1 //(2,1) (2,0), (2,-1), (2,-2) ... (-2,-2) //(-2,-1) (-2,0) (-2,1) (-2,2) (-2,3) //(0,1) (1,-1) (-1,2) (2,-2) (-2,3) public static int solution(int X, int Y){ int count=0; int x=0, y=0; int dx=-1; int dy=0; int temp=0; for(int i=0; i<Math.pow(Math.max(X, Y)+2, 2); i++){ System.out.println(dx + ", " + dy); if(x==X && y==Y) return count; count++; if(x == y){ temp = dy; dy = -dx; dx = temp; } else if( ((x==0)&&(y==1)) || ((x>0)&&(Math.abs(x)==Math.abs(y))) || ((x<0) && (x==1-y))){ temp = dx; dx = dy; dy = temp; } x += dx; y += dy; } return count; } }
첨부파일 Task2.java (1,222byte)