---
# System prepended metadata

title: zero judge K731解題報告

---

# zero judge K731解題報告
題目連結：
https://zerojudge.tw/ShowProblem?problemid=k731

解題思路：
外積代表兩項量所張出的平行四邊形面積，計算公式為$x1*y2 - x2*y1$。  
計算兩項量外積，大於0代表向左，小於0代表向右，等於0代表迴轉或**沒改變方向**  
![Screenshot_20240317_235708_Samsung_Notes](https://hackmd.io/_uploads/H1D9KcN0a.jpg)

掌握這個關鍵就可以輕鬆解題啦！
```python!
n = int(input())
vectors = []
last = [0, 0]
left = 0
right = 0
ret = 0
#將所有座標輸入轉換成向量
for i in range(n):
    x, y = map(int, input().split())
    vectors.append((x - last[0], y - last[1]))
    last = [x, y]

for i in range(1, len(vectors)):
    x1, y1 = vectors[i - 1]
    x2, y2 = vectors[i]
    #內積 > 0代表方向沒有更改，直接跳過
    if x1 * x2 + y1 * y2 > 0:
        continue
    
    product = x1 * y2 - x2 * y1#計算外積
    if product > 0:
        left += 1
    elif product < 0:
        right += 1
    else:
        ret += 1
print(left, right, ret)

```