样例对的,结果全WA

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

HarryPotterJames @ 2020-08-30 13:11:32

#include<bits/stdc++.h>
using namespace std;
int main()
{
    double x1,x2,y1,y2,z1,z2;
    cin>>x1>>x2>>y1>>y2>>z1>>z2;
    double a1=sqrt(pow(x1-x2,2)*pow(y1-y2,2));
    double b1=sqrt(pow(x1-x2,2)*pow(z1-z2,2));
    double c1=sqrt(pow(y1-y2,2)*pow(z1-z2,2));
    double sum=a1+b1+c1;
    cout<<fixed<<setprecision(2)<<sum;
    return 0;
}

by ⚡小林子⚡ @ 2020-08-30 13:14:24

@HarryPotterJames 而且两点公式是 sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)),公式写错了


by ⚡小林子⚡ @ 2020-08-30 13:17:46

@andyli 我不怎么喜欢用(((


by HarryPotterJames @ 2020-08-30 13:18:41

还是不行


by HarryPotterJames @ 2020-08-30 13:19:09

@⚡小林子⚡


by HarryPotterJames @ 2020-08-30 13:20:52

#include<bits/stdc++.h>
using namespace std;
int dis(double x1,double x2,double y1,double y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int main()
{
    double x1,x2,y1,y2,z1,z2;
    cin>>x1>>x2>>y1>>y2>>z1>>z2;
    double a1=dis(x1,x2,y1,y2);
    double b1=dis(y1,y2,z1,z2);
    double c1=dis(x1,x2,z1,z2);
    cout<<fixed<<setprecision(2)<<a1+b1+c1;
    return 0;
}

by ⚡小林子⚡ @ 2020-08-30 13:28:45

@HarryPotterJames


by Sya_Resory @ 2020-08-30 13:29:22

@HarryPotterJames 万能头里定义了y1,换一个变量名试试


by ⚡小林子⚡ @ 2020-08-30 13:29:37

@HarryPotterJames

#include<bits/stdc++.h>
using namespace std;
double dis(double x1,double x2,double y1,double y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int main()
{
    double x1,x2,y1,y2,z1,z2;
    cin>>x1>>x2>>y1>>y2>>z1>>z2;
    double a1=dis(x1,y1,x2,y2);
    double b1=dis(y1,z1,y2,z2);
    double c1=dis(z1,x1,z2,x2);
    cout<<fixed<<setprecision(2)<<a1+b1+c1;
    return 0;
}

by ⚡小林子⚡ @ 2020-08-30 13:31:11

@HarryPotterJames 以后写距离公式传参的时候不建议把行的参数和列的参数放一起,这样很容易弄混。

最好是把每一个点的坐标参数放一起,也就是

double dis(double x1,double y1,double x2,double y2)

这样逻辑会清晰很多


by Prean @ 2020-08-30 13:39:03

@⚡小林子⚡ 他写的没错,用pow是一样的(


| 下一页