洛谷刷题C++语言 | P5735 距离函数

学习C++从娃娃抓起!记录下洛谷C++学习和备考过程中的题目,记录每一个瞬间。

附上汇总贴:洛谷刷题C++语言 | 汇总_热爱编程的通信人的博客-CSDN博客


【题目描述】

给出平面坐标上不在一条直线上三个点坐标 (x1,y1),(x2,y2),(x3,y3),坐标值是实数,且绝对值不超过 100.00,求围成的三角形周长。保留两位小数。

对于平面上的两个点 (x1,y1),(x2,y2),则这两个点之间的距离

【输入】

输入三行,第 i 行表示坐标 (xi,yi),以一个空格隔开。

【输出】

输出一个两位小数,表示由这三个坐标围成的三角形的周长。

【输入样例】

0 0
0 3
4 0

【输出样例】

12.00

【代码详解】

#include <bits/stdc++.h>
using namespace std;
double dis(double x1, double y1, double x2, double y2);
int main()
{
    double x1, x2, x3, y1, y2, y3, ab, ac, bc, ans;
    cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
    ab = dis(x1, y1, x2, y2);
    ac = dis(x1, y1, x3, y3);
    bc = dis(x2, y2, x3, y3);
    ans = ab + ac + bc;
    printf("%.2f", ans);
    return 0;
}
double dis(double x1, double y1, double x2, double y2) 
{
    double ans;
    ans = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
    ans = sqrt(ans);
    return ans;
}

【代码运行】

0 0
0 3
4 0
12.00

猜你喜欢

转载自blog.csdn.net/guolianggsta/article/details/132788641
今日推荐