{ }codelper
defining functionsP0435

A running course made of three flags

A PE teacher plants three flags on the playground to make a triangular running course.
The playground is a coordinate plane, and each flag is one point (x, y) with integer coordinates.
The squared distance between two points (x1, y1) and (x2, y2) is (x1 − x2)² + (y1 − y2)².
Find the squared length of each of the three sides of the course; let c be the largest of the three values and s the sum of the other two.
If the largest value appears more than once, take just one of them as c (s is the same whichever you pick).
If s = c, the course is a right triangle, so print "right".
If s > c, it is an acute triangle, so print "acute".
If s < c, it is an obtuse triangle, so print "obtuse".
The first line gives the number of courses n.
Each of the next n lines gives one course as six integers x1 y1 x2 y2 x3 y3, the coordinates of its three flags, separated by single spaces.
The three flags never lie on one straight line.
So no two flags are ever at the same spot.
Print the verdict for each course on its own line, in input order.
Write a function that receives the coordinates of two points and returns their squared distance, and call it three times, once for each side.
Grading looks only at the output.

Input
1 ≤ n ≤ 1,000, -1,000 ≤ every coordinate ≤ 1,000 (all integers), the three flags never lie on one straight line
Output
n lines, each one of right, acute, obtuse
Example 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
right
right
acute
obtuse
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

This problem practises calling one function three times.
dist2(x1, y1, x2, y2) fills its parameters in the order the values are passed, so the three calls differ only in which two points they pass.
The frames are Python def dist2(x1, y1, x2, y2):, JS function dist2(x1, y1, x2, y2), TS with : number after the parentheses, Java static int dist2(int x1, …), C# static int Dist2(int x1, …), C and C++ int dist2(int x1, …), and Craykon dist2(x1, y1, x2, y2) { … return dx * dx + dy * dy; }.
Coordinates stay within ±1,000, so a squared distance is at most 2,000² + 2,000² = 8,000,000, and even the sum of three sides fits easily in an int.
Picking the largest value also differs.
Python max(a, b, c) and JS/TS Math.max(a, b, c) take all three at once, and C++ wraps them in braces as std::max({a, b, c}).
Java Math.max, C# Math.Max and Craykon Mathf.Max take only two, so you nest them as Math.max(a, Math.max(b, c)), and C has no standard max function for integers (fmax is for floating-point numbers), so you compare with if.
Trap one: finding the side lengths with a square root and squaring them again to compare.
A length like √2 cannot be stored exactly as a floating-point number, so in Python math.sqrt(2) ** 2 is 2.0000000000000004.
As a result the smallest right triangle (0, 0), (1, 0), (0, 1) comes out obtuse, and the second course of the example becomes 16.000000000000004 > 16.0, so acute.
Comparing the squared distances, which are integers, without any square root leaves no error.
Trap two: always treating the third side (between the third flag and the first) as the longest.
In the first course of the example the third side's square is 9, the shortest, so it comes out acute, and so does the fourth course.
Trap three: writing the square as dx ^ 2.
In Python, JS, TS, Java, C#, C and C++, ^ on integers is bitwise XOR, not a power, so 3 ^ 2 is 1.
(dx ^ 2) + (dy ^ 2) compiles as is in all seven languages, and the judge's gcc 11 gives no warning even with -Wall.
A C solution written this way judges all four courses of the example obtuse.
Written without parentheses as dx ^ 2 + dy ^ 2, + binds first and it becomes dx ^ (2 + dy) ^ 2; gcc 11 with -Wall only warns that you should add parentheses.
The only power operator is ** in Python and JS/TS; Java's Math.pow, C#'s Math.Pow and C/C++'s pow return floating-point numbers.
Craykon has no ^ operator, so it is a compile error.
Squaring by multiplying, as in dx * dx, is the surest way in every language.

Pythonmain.py · 22 lines
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("right")    elif rest > big:        print("acute")    else:        print("obtuse")
JavaScriptmain.js · 23 lines
// 두 점 사이 거리의 제곱을 돌려준다. 제곱근을 구하지 않으므로 늘 정수다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("right");  else if (rest > big) out.push("acute");  else out.push("obtuse");}console.log(out.join("\n"));
TypeScriptmain.ts · 25 lines
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("right");  else if (rest > big) out.push("acute");  else out.push("obtuse");}console.log(out.join("\n"));
JavaMain.java · 35 lines
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("right\n");            else if (rest > big) out.append("acute\n");            else out.append("obtuse\n");        }        System.out.print(out);    }}
C#Main.cs · 36 lines
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("right\n");            else if (rest > big) sb.Append("acute\n");            else sb.Append("obtuse\n");        }        Console.Write(sb);    }}
C++main.cpp · 29 lines
#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 << "right\n";        else if (rest > big) std::cout << "acute\n";        else std::cout << "obtuse\n";    }}
Cmain.c · 29 lines
#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("right");        else if (rest > big) puts("acute");        else puts("obtuse");    }    return 0;}
Craykonmain.cray · 31 lines
// 두 점 사이 거리의 제곱을 돌려준다. 제곱근을 구하지 않으므로 늘 정수다거리제곱(x1, y1, x2, y2){    dx = x1 - x2;    dy = y1 - y2;    return dx * dx + dy * dy;} n = Console.ReadLine().ToNumber();for (i = 0; i < n; i++){    p = Console.ReadNumbers();    // 같은 함수를 세 번 부른다: 변마다 두 점만 바뀐다    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 = Mathf.Max(a, Mathf.Max(b, c));    rest = a + b + c - big; // 나머지 두 변의 제곱의 합    if (rest == big)    {        Console.WriteLine("right");    }    else if (rest > big)    {        Console.WriteLine("acute");    }    else    {        Console.WriteLine("obtuse");    }}