코테/백준

[백준/JAVA] 11758번: CCW

imname1am 2023. 5. 18. 13:24
반응형

🔺 문제

 

11758번: CCW

첫째 줄에 P1의 (x1, y1), 둘째 줄에 P2의 (x2, y2), 셋째 줄에 P3의 (x3, y3)가 주어진다. (-10,000 ≤ x1, y1, x2, y2, x3, y3 ≤ 10,000) 모든 좌표는 정수이다. P1, P2, P3의 좌표는 서로 다르다.

www.acmicpc.net

 

 

🔺 코드

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
import java.util.*;
import java.io.*;
 
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine()," ");
           
        int x1 = Integer.parseInt(st.nextToken());
        int y1 = Integer.parseInt(st.nextToken());
        
        st = new StringTokenizer(br.readLine()," ");   
        int x2 = Integer.parseInt(st.nextToken());
        int y2 = Integer.parseInt(st.nextToken());
        
        st = new StringTokenizer(br.readLine()," ");   
        int x3 = Integer.parseInt(st.nextToken());
        int y3 = Integer.parseInt(st.nextToken());
        
        int ccw = (x1*y2 + x2*y3 + x3*y1) - (x2*y1 + x3*y2 + x1*y3);
        
        if(ccw < 0)       System.out.println(-1);   // 시계 방향
        else if(ccw == 0System.out.println(0);    // 일직선
        else              System.out.println(1);    //  방향
    }
}
cs
✅ 해결 아이디어
✔ CCW (counter-clockwise)

 

 


💬 느낀 점

앞에서 복잡(?)한 거 풀다가 이런거 푸니까 맘이 편안하고 막 그래...

 

 

1회독 2회독 3회독 4회독 5회독
V 7/11      

 

 

(+7/11 2회독)

더보기

 

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
import java.util.*;
import java.io.*;
 
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        
        int[][] pos = new int[3][2];
        
        for(int i = 0 ; i < 3 ; i++) {
            st = new StringTokenizer(br.readLine(), " ");
            pos[i][0= Integer.parseInt(st.nextToken());
            pos[i][1= Integer.parseInt(st.nextToken());
        }
        
        long tmp1 = (pos[0][0* pos[1][1+ pos[1][0* pos[2][1+ pos[2][0* pos[0][1]);
        long tmp2 = (pos[1][0* pos[0][1+ pos[2][0* pos[1][1+ pos[0][0* pos[2][1]);
        
        long ccw = tmp1 - tmp2;
        long answer = 0;
        
        if(ccw > 0) answer = 1;
        else if(ccw < 0) answer = -1;
        
        System.out.println(answer);
    }  
}
 
cs

 


(참고)

✔ Do it 알고리즘 코딩테스트 자바편

 

반응형