ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Triangle Classification

Classify three side lengths as invalid, equilateral, isosceles, or scalene.

สิ่งที่คุณจะได้เรียนรู้

  • ตรวจอสมการสามเหลี่ยม
  • จัดประเภทตามด้านที่ยาวเท่ากัน
  • เรียงเงื่อนไขจากเฉพาะเจาะจงไปทั่วไป

โจทย์

Classify three side lengths as invalid, equilateral, isosceles, or scalene. A valid triangle has positive sides, and its two shortest sides must sum to more than its longest side. Equal-to is invalid: it forms a straight line. Use equilateral for three equal sides, isosceles for exactly two, and scalene for three different sides.

Implement triangle_classification(sides).

Input

sides: The three side lengths in any order.

Output

Return one lowercase label: invalid, equilateral, isosceles, or scalene.

เงื่อนไข

Exactly three integer sides, each between −1,000,000,000 and 1,000,000,000. Nonpositive sides are invalid.

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
sides = [5, 5, 8]

ผลลัพธ์ที่คาดหวัง:
expected = "isosceles"
อินพุต:
std::vector<long long> sides = {5, 5, 8};

ผลลัพธ์ที่คาดหวัง:
std::string expected = "isosceles";
อินพุต:
long[] sides = new long[] {5, 5, 8};

ผลลัพธ์ที่คาดหวัง:
string expected = "isosceles";

คำอธิบาย

Two sides equal 5, and 5 + 5 > 8, so this is a valid isosceles triangle.

ตัวอย่างที่ 2
อินพุต:
sides = [1, 2, 3]

ผลลัพธ์ที่คาดหวัง:
expected = "invalid"
อินพุต:
std::vector<long long> sides = {1, 2, 3};

ผลลัพธ์ที่คาดหวัง:
std::string expected = "invalid";
อินพุต:
long[] sides = new long[] {1, 2, 3};

ผลลัพธ์ที่คาดหวัง:
string expected = "invalid";

คำอธิบาย

1 + 2 equals 3. The strict inequality fails, so these lengths form a line.

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

ตัดสามเหลี่ยมที่เป็นไปไม่ได้ออกก่อน: ด้านไม่เป็นบวก หรือด้านที่ยาวสุดยาวเกินไป

คำใบ้ 2

เรียงด้านก่อนเพื่อเช็กอสมการสามเหลี่ยมได้ง่าย

คำใบ้ 3

จากนั้นจำแนกตามจำนวนด้านที่เท่ากัน: สาม ด้าน สองด้าน หรือไม่เท่ากันเลย

ความซับซ้อนที่คาดหวัง

เวลา O(1), หน่วยความจำเพิ่ม O(1)

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก