满江红·写怀(发代码,栓Q)

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

kouzeyang @ 2024-06-27 17:20:41

#include <bits/stdc++.h>
using namespace std;

double jl(int x1,int y1,int x2,int y2)
{
    int dis;
    dis=sqrt(abs((x2-x1))*abs((x2-x1))+abs((y2-y1))*abs((y2-y1)));
    return dis;
} 

int main()
{
    int x1,x2,x3,y1,y2,y3;
    cin>>x1>>y1>>x2>>y2>>x3>>y3;
    cout<<jl(x1,y1,x2,y2)+jl(x1,y1,x3,y3)+jl(x2,y2,x3,y3);
    return 0;
}

by WoodReal12 @ 2024-06-27 17:23:15

@kouzeyang 哥,你开double啊


by qazsedcrfvgyhnujijn @ 2024-06-27 17:24:09

#include <bits/stdc++.h>
using namespace std;

struct Point{
    double x, y;
}a, b, c;
int main(){
    cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y;
    double ans = 0;
    ans += sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
    ans += sqrt(pow(b.x - c.x, 2) + pow(b.y - c.y, 2));
    ans += sqrt(pow(a.x - c.x, 2) + pow(a.y - c.y, 2));
    printf("%.2f", ans);
    return 0;
}

有更简单的写法,建议去学 hypot


by kouzeyang @ 2024-06-27 17:26:47

@WoodReal12 OK


by Jason101 @ 2024-08-14 13:15:47


#include<bits/stdc++.h>
using namespace std;
int main()
{
    double x1,y1,x2,y2,x3,y3;
    cin >> x1 >> y1;
    cin >> x2 >> y2;
    cin >> x3 >> y3;
    double l1 = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); 
    double l2 = sqrt((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1)); 
    double l3 = sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2)); 
    printf("%.2lf",l1 + l2 + l3);
    return 0;
}

|