Search

평행

알고리즘
연습문제
플랫폼
프로그래머스
JCF
상태
해결
생성 일시
2023/12/26 12:02
최종 편집 일시
2023/12/27 08:47

문제 설명

해결과정

Solution.java

import java.util.*; class Solution { public int solution(int[][] dots) { int answer = 0; // 0번 인덱스와 연결할 인덱스 int index = 1; while(index < 4) { double firstScope = calCFirstScope(dots, index); // 나머지 인덱스 연결 double secondScope = calCSecondScope(dots, index); if(firstScope == secondScope) { answer = 1; break; } index++; } return answer; } // 나머지 인덱스 연결 기울기 반환 public double calCSecondScope(int[][] dots, int index) { boolean check = false; // 첫번째 X좌표 int firstX = 0; // 첫번째 Y좌표 int firstY = 0; // 두번째 X좌표 int secondX = 0; // 두번째 Y좌표 int secondY = 0; for(int i = 1; i < 4; i++) { if(i != index && !check) { check = true; firstX = dots[i][0]; firstY = dots[i][1]; } else if (i != index && check){ secondX = dots[i][0]; secondY = dots[i][1]; } } double secondScope = 0; if (secondX < firstX) { secondScope = (double) (firstY-secondY) / (double) (firstX-secondX); } else { secondScope = (double) (secondY-firstY) / (double) (secondX-firstX); } return secondScope; } // 현재 인덱스와 0번 인덱스의 기울기 구하기 public double calCFirstScope(int[][] dots, int index) { // 0번인덱스의 x좌표 int zeroX = dots[0][0]; // 0번인덱스의 Y좌표 int zeroY = dots[0][1]; // 인덱스의 x좌표 int indexX = dots[index][0]; // 인덱스의 Y좌표 int indexY = dots[index][1]; // 첫번째 기울기 double firstScope = 0; if (zeroX < indexX) { firstScope = (double) (indexY-zeroY) / (double) (indexX-zeroX); } else { firstScope = (double) (zeroY-indexY) / (double) (zeroX-indexX); } return firstScope; } public void squareArrayPrint(int[][] arrs) { for(int i = 0; i < arrs.length; i++) { for(int j = 0; j < arrs[i].length; j++) { System.out.print(arrs[i][j] + " "); } System.out.println(); } } }
Java
복사