{ }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++ のどれでも整数の ^ はべき乗ではなくビット XOR なので、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)    {        コンソール.書く("鋭角");    }    でなければ    {        コンソール.書く("鈍角");    }}