1 条题解

  • 0
    @ 2026-10-20 23:55:56

    【深基7.例1】距离函数

    解题思路

    对三条边分别使用两点距离公式,再相加。用 double 保存实数。

    正确性说明

    欧氏距离公式得到每条边长,三角形周长按定义就是三边长度之和。

    易错点

    用 fixed 与 setprecision(2) 保留两位小数;横纵坐标都必须参与计算。

    复杂度

    时间 O(1),空间 O(1)。

    参考代码(C++11)

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        double x[3], y[3], ans = 0;
        for (int i = 0; i < 3; i++)
            cin >> x[i] >> y[i];
        for (int i = 0; i < 3; i++) {
            int j = (i + 1) % 3;
            ans += hypot(x[i] - x[j], y[i] - y[j]);
        }
        cout << fixed << setprecision(2) << ans << "\n";
        return 0;
    }
    
    • 1