{ }codelper
定义函数P0435

三面旗子围成的跑步路线

体育老师在操场上插三面旗子,围成一条三角形的跑步路线。
操场是坐标平面,每面旗子是一个坐标为整数的点 (x, y)。
两点 (x1, y1) 和 (x2, y2) 之间距离的平方是 (x1 − x2)² + (y1 − y2)²。
求出路线三条边各自长度的平方,设其中最大的值为 c,其余两个值之和为 s。
如果最大的值有两个或以上,只取其中一个作为 c(无论取哪个,s 都一样)。
若 s = c,路线是直角三角形,输出 "直角"。
若 s > c,是锐角三角形,输出 "锐角"。
若 s < c,是钝角三角形,输出 "钝角"。
第一行给出路线数 n。
接下来 n 行,每行给出一条路线三面旗子的坐标 x1 y1 x2 y2 x3 y3,共六个整数,用一个空格隔开。
三面旗子不会在同一条直线上。
所以也不会有两面旗子在同一位置。
按输入顺序,每行输出一条路线的判定。
编写一个函数,接收两点的坐标并返回距离的平方,对三条边各调用一次,共调用三次。
评测只看输出。

输入
1 ≤ n ≤ 1,000,-1,000 ≤ 所有坐标 ≤ 1,000(均为整数),三面旗子不在同一条直线上
输出
n 行,每行是 right、acute、obtuse 之一
样例 1 · stdin
4
0 0 4 0 0 3
1 1 3 3 5 1
0 0 4 0 2 3
0 0 6 0 1 1
stdout
直角
直角
锐角
钝角
main.py
输入(stdin)

运行时这些行会依次作为 input()/标准输入传入(运行中不会等待输入)。

$ 就绪

请在电脑上打开以编写和运行代码。在手机上可以查看题目和题解。

其他语言怎么写?查看各语言题解 · 8 种语言题解

这道题练习把一个函数调用三次。
dist2(x1, y1, x2, y2) 按传入的顺序把值放进参数,所以三次调用只需改变传入的两个点。
定义的写法是 Python def dist2(x1, y1, x2, y2):,JS function dist2(x1, y1, x2, y2),TS 在括号后写 : number,Java static int dist2(int x1, …),C# static int Dist2(int x1, …),C、C++ int dist2(int x1, …),Craykon 距离平方(x1, y1, x2, y2) { … 返回 dx * dx + dy * dy; }。
坐标在 ±1,000 以内,距离的平方最多是 2,000² + 2,000² = 8,000,000,三条边加起来也完全装得进 int。
选出最大值的方法也不同。
Python max(a, b, c) 和 JS、TS Math.max(a, b, c) 一次接收三个,C++ 要像 std::max({a, b, c}) 这样用花括号括起来。
Java Math.max、C# Math.Max、Craykon 数学.较大值 只接收两个,要像 Math.max(a, Math.max(b, c)) 这样嵌套,C 没有用于整数的标准 max 函数(fmax 用于浮点数),要用 if 比较。
陷阱一:先用平方根求出边长,再平方回去比较。
像 √2 这样的长度无法用浮点数精确表示,所以在 Python 中 math.sqrt(2) ** 2 是 2.0000000000000004。
结果最小的直角三角形 (0, 0)、(1, 0)、(0, 1) 被判成 obtuse,示例的第二条路线变成 16.000000000000004 > 16.0,被判成 acute。
不求平方根,直接比较距离的平方这些整数,就不会有误差。
陷阱二:总把第三条边(第三面旗子和第一面旗子之间)当作最长边。
示例的第一条路线第三条边的平方是 9,最短,于是被判成 acute,第四条路线也被判成 acute。
陷阱三:把平方写成 dx ^ 2。
在 Python、JS、TS、Java、C#、C、C++ 中,整数的 ^ 都不是乘方而是按位异或,所以 3 ^ 2 是 1。
(dx ^ 2) + (dy ^ 2) 在这七种语言中都能直接编译,评测机的 gcc 11 即使加 -Wall 也不警告。
这样写的 C 解答把示例的四条路线全部判成 obtuse。
不加括号写成 dx ^ 2 + dy ^ 2 时,+ 先结合,变成 dx ^ (2 + dy) ^ 2,gcc 11 在 -Wall 下只提示加括号。
乘方运算符只有 Python 和 JS、TS 的 **,Java、C#、C、C++ 的 Math.pow、Math.Pow、pow 返回浮点数。
Craykon 没有 ^ 运算符,会出现编译错误。
像 dx * dx 这样用乘法求平方,在任何语言中都最可靠。

Pythonmain.py · 22 行
def dist2(x1, y1, x2, y2):    # 두 점 사이 거리의 제곱을 돌려준다. 제곱근을 구하지 않으므로 늘 정수다    dx = x1 - x2    dy = y1 - y2    return dx * dx + dy * dy  n = int(input())for _ in range(n):    x1, y1, x2, y2, x3, y3 = map(int, input().split())    # 같은 함수를 세 번 부른다: 변마다 두 점만 바뀐다    a = dist2(x1, y1, x2, y2)    b = dist2(x2, y2, x3, y3)    c = dist2(x3, y3, x1, y1)    big = max(a, b, c)    rest = a + b + c - big  # 나머지 두 변의 제곱의 합    if rest == big:        print("直角")    elif rest > big:        print("锐角")    else:        print("钝角")
JavaScriptmain.js · 23 行
// 두 점 사이 거리의 제곱을 돌려준다. 제곱근을 구하지 않으므로 늘 정수다function dist2(x1, y1, x2, y2) {  const dx = x1 - x2;  const dy = y1 - y2;  return dx * dx + dy * dy;} const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);const out = [];for (let i = 1; i <= n; i++) {  const [x1, y1, x2, y2, x3, y3] = lines[i].trim().split(/\s+/).map(Number);  // 같은 함수를 세 번 부른다: 변마다 두 점만 바뀐다  const a = dist2(x1, y1, x2, y2);  const b = dist2(x2, y2, x3, y3);  const c = dist2(x3, y3, x1, y1);  const big = Math.max(a, b, c);  const rest = a + b + c - big; // 나머지 두 변의 제곱의 합  if (rest === big) out.push("直角");  else if (rest > big) out.push("锐角");  else out.push("钝角");}console.log(out.join("\n"));
TypeScriptmain.ts · 25 行
import { readFileSync } from "node:fs"; // 두 점 사이 거리의 제곱을 돌려준다. 괄호 뒤의 `: number`가 돌려주는 값의 타입이다function dist2(x1: number, y1: number, x2: number, y2: number): number {  const dx = x1 - x2;  const dy = y1 - y2;  return dx * dx + dy * dy;} const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);const out: string[] = [];for (let i = 1; i <= n; i++) {  const [x1, y1, x2, y2, x3, y3]: number[] = lines[i].trim().split(/\s+/).map(Number);  // 같은 함수를 세 번 부른다: 변마다 두 점만 바뀐다  const a: number = dist2(x1, y1, x2, y2);  const b: number = dist2(x2, y2, x3, y3);  const c: number = dist2(x3, y3, x1, y1);  const big: number = Math.max(a, b, c);  const rest: number = a + b + c - big; // 나머지 두 변의 제곱의 합  if (rest === big) out.push("直角");  else if (rest > big) out.push("锐角");  else out.push("钝角");}console.log(out.join("\n"));
JavaMain.java · 35 行
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.StringTokenizer; public class Main {    // 두 점 사이 거리의 제곱을 돌려준다. 이름 앞의 int가 돌려주는 값의 타입이다    static int dist2(int x1, int y1, int x2, int y2) {        int dx = x1 - x2;        int dy = y1 - y2;        return dx * dx + dy * dy;    }     public static void main(String[] args) throws IOException {        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        int n = Integer.parseInt(br.readLine().trim());        StringBuilder out = new StringBuilder();        for (int i = 0; i < n; i++) {            StringTokenizer st = new StringTokenizer(br.readLine());            int x1 = Integer.parseInt(st.nextToken()), y1 = Integer.parseInt(st.nextToken());            int x2 = Integer.parseInt(st.nextToken()), y2 = Integer.parseInt(st.nextToken());            int x3 = Integer.parseInt(st.nextToken()), y3 = Integer.parseInt(st.nextToken());            // 같은 함수를 세 번 부른다: 변마다 두 점만 바뀐다            int a = dist2(x1, y1, x2, y2);            int b = dist2(x2, y2, x3, y3);            int c = dist2(x3, y3, x1, y1);            int big = Math.max(a, Math.max(b, c));            int rest = a + b + c - big; // 나머지 두 변의 제곱의 합            if (rest == big) out.append("直角\n");            else if (rest > big) out.append("锐角\n");            else out.append("钝角\n");        }        System.out.print(out);    }}
C#Main.cs · 36 行
using System;using System.Text; class Program{    // 두 점 사이 거리의 제곱을 돌려준다. 이름 앞의 int가 돌려주는 값의 타입이다    static int Dist2(int x1, int y1, int x2, int y2)    {        int dx = x1 - x2;        int dy = y1 - y2;        return dx * dx + dy * dy;    }     static void Main()    {        int n = int.Parse(Console.ReadLine());        var sb = new StringBuilder();        for (int i = 0; i < n; i++)        {            string[] p = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);            int x1 = int.Parse(p[0]), y1 = int.Parse(p[1]);            int x2 = int.Parse(p[2]), y2 = int.Parse(p[3]);            int x3 = int.Parse(p[4]), y3 = int.Parse(p[5]);            // 같은 함수를 세 번 부른다: 변마다 두 점만 바뀐다            int a = Dist2(x1, y1, x2, y2);            int b = Dist2(x2, y2, x3, y3);            int c = Dist2(x3, y3, x1, y1);            int big = Math.Max(a, Math.Max(b, c));            int rest = a + b + c - big; // 나머지 두 변의 제곱의 합            if (rest == big) sb.Append("直角\n");            else if (rest > big) sb.Append("锐角\n");            else sb.Append("钝角\n");        }        Console.Write(sb);    }}
C++main.cpp · 29 行
#include <algorithm>#include <iostream> // 두 점 사이 거리의 제곱을 돌려준다. 이름 앞의 int가 돌려주는 값의 타입이다int dist2(int x1, int y1, int x2, int y2) {    int dx = x1 - x2;    int dy = y1 - y2;    return dx * dx + dy * dy;} int main() {    std::ios::sync_with_stdio(false);    std::cin.tie(nullptr);    int n;    std::cin >> n;    for (int i = 0; i < n; i++) {        int x1, y1, x2, y2, x3, y3;        std::cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;        // 같은 함수를 세 번 부른다: 변마다 두 점만 바뀐다        int a = dist2(x1, y1, x2, y2);        int b = dist2(x2, y2, x3, y3);        int c = dist2(x3, y3, x1, y1);        int big = std::max({a, b, c});        int rest = a + b + c - big;  // 나머지 두 변의 제곱의 합        if (rest == big) std::cout << "直角\n";        else if (rest > big) std::cout << "锐角\n";        else std::cout << "钝角\n";    }}
Cmain.c · 29 行
#include <stdio.h> // 두 점 사이 거리의 제곱을 돌려준다. 이름 앞의 int가 돌려주는 값의 타입이다int dist2(int x1, int y1, int x2, int y2) {    int dx = x1 - x2;    int dy = y1 - y2;    return dx * dx + dy * dy;  // C에는 거듭제곱 연산자가 없다. ^는 XOR이다} int main(void) {    int n;    if (scanf("%d", &n) != 1) return 0;    for (int i = 0; i < n; i++) {        int x1, y1, x2, y2, x3, y3;        if (scanf("%d %d %d %d %d %d", &x1, &y1, &x2, &y2, &x3, &y3) != 6) return 0;        // 같은 함수를 세 번 부른다: 변마다 두 점만 바뀐다        int a = dist2(x1, y1, x2, y2);        int b = dist2(x2, y2, x3, y3);        int c = dist2(x3, y3, x1, y1);        int big = a;        if (b > big) big = b;        if (c > big) big = c;        int rest = a + b + c - big;  // 나머지 두 변의 제곱의 합        if (rest == big) puts("直角");        else if (rest > big) puts("锐角");        else puts("钝角");    }    return 0;}
Craykonmain.cray · 31 行
// 두 점 사이 거리의 제곱을 돌려준다. 제곱근을 구하지 않으므로 늘 정수다거리제곱(x1, y1, x2, y2){    dx = x1 - x2;    dy = y1 - y2;    返回 dx * dx + dy * dy;} n = 控制台.读行().转数字();循环 (i = 0; i < n; i++){    p = 控制台.读数字();    // 같은 함수를 세 번 부른다: 변마다 두 점만 바뀐다    a = 거리제곱(p[0], p[1], p[2], p[3]);    b = 거리제곱(p[2], p[3], p[4], p[5]);    c = 거리제곱(p[4], p[5], p[0], p[1]);    big = 数学.较大值(a, 数学.较大值(b, c));    rest = a + b + c - big; // 나머지 두 변의 제곱의 합    如果 (rest == big)    {        控制台.输出("直角");    }    否则 如果 (rest > big)    {        控制台.输出("锐角");    }    否则    {        控制台.输出("钝角");    }}