GESP Level 2 C++ Practice (Mathematical Functions) luogu-B3638, Triangle Area

GESP Level 2 practice, mathematical function exercises, difficulty ★✮☆☆☆.

  • GESP Level 2 Practice Question List

  • GESP Level 2 Real Exam Questions List

  • GESP Level 2 Exam Syllabus Analysis

luogu-B3638 Triangle Area

Problem Requirements

Problem Description

Given the coordinates of three integer points in a Cartesian coordinate system, calculate the area of the triangle formed by these points.

The data guarantees that the answer will always be an integer. Therefore, if you use floating-point numbers for calculations, please round to the nearest integer..

Distance formula between two points: The distance between

Heron’s formula: If the lengths of the sides of the triangle are , then the area of the triangle is , where .

Input Format

There are three lines in total, each line represents a point on the triangle. Each line contains two positive integers representing the coordinates of the point, in the form of <span>x y</span>.

Output Format

One line, an integer representing the area of the triangle.

Sample Input #1

10 20
30 40
50 50

Sample Output #1

100

Sample Explanation

The area can be calculated using Heron’s formula. The method is as follows.

Distance: Distance: Distance:

Using Heron’s formula, calculate the approximate area: , thus the answer is .

Data Scale and Conventions

For data: the coordinate values of each point are guaranteed to be within and are all integers; the answer is guaranteed to be a positive integer.

Problem Analysis

  1. Read the coordinates of the three points
  2. Use the sqrt function to calculate the distances between the three points
  3. Calculate the semi-perimeter of the triangle
  4. Use the sqrt function to calculate the area of the triangle
  5. Output the area of the triangle

Sample Code

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
    // Read the coordinates of three points
    int x1, y1, x2, y2, x3, y3;
    cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
    // Calculate the distances between the three points
    double a, b, c;
    a = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); // Calculate AB distance
    b = sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)); // Calculate AC distance
    c = sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3)); // Calculate BC distance
    // Calculate the semi-perimeter of the triangle
    double tmp = (a + b + c) / 2;
    // Calculate the area of the triangle
    double s = sqrt(tmp * (tmp - a) * (tmp - b) * (tmp - c));
    // Output the area of the triangle
    printf("%.0f", s);
    return 0;
}

For detailed GESP syllabus, real exam question explanations, knowledge expansion, and practice lists, see:

[Pinned] [GESP] C++ Certification Learning Resource Summary

luogu- series questions can be evaluated online at Luogu Question Bank.

bcqm- series questions can be evaluated online at Programming Enlightenment Question Bank.

Leave a Comment