1 条题解

  • 0
    @ 2025-6-3 22:02:29

    题意分析

    题目给出一个平行四边形两个相邻边的四个坐标,要求求出第四个顶点的坐标。

    解题思路

    因为是两个相邻边的四个坐标,因此必定有两个坐标是重复的。假设这个坐标是A,其余两个已知坐标是B和C。那么第四个顶点坐标就是D=B+CAD = B+C-A(向量表示)

    标程

    #include <iostream>
    #include <sstream>
    #include <cmath>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    bool points_equal(double x1, double y1, double x2, double y2) {
        return fabs(x1 - x2) < 1e-5 && fabs(y1 - y2) < 1e-5;
    }
    
    double adjust(double val) {
        double r = round(val * 1000.0) / 1000.0;  // 四舍五入到毫米
        if (fabs(r) < 0.0005) return 0.0;         // 消除-0.000
        return r;
    }
    
    int main() {
        string line;
        while (getline(cin, line)) {
            if (line.empty()) continue;
            istringstream iss(line);
            double x1, y1, x2, y2, x3, y3, x4, y4;
            iss >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;
            
            double x, y; // 第四个顶点坐标
            
            if (points_equal(x1, y1, x3, y3)) {
                x = x2 + x4 - x1;
                y = y2 + y4 - y1;
            } else if (points_equal(x1, y1, x4, y4)) {
                x = x2 + x3 - x1;
                y = y2 + y3 - y1;
            } else if (points_equal(x2, y2, x3, y3)) {
                x = x1 + x4 - x2;
                y = y1 + y4 - y2;
            } else { // points_equal(x2, y2, x4, y4)
                x = x1 + x3 - x2;
                y = y1 + y3 - y2;
            }
    
            x = adjust(x);
            y = adjust(y);
            cout << fixed << setprecision(3) << x << " " << y << endl;
        }
        return 0;
    }
    
    • 1

    信息

    ID
    1625
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    1
    已通过
    1
    上传者