Why it's wrong?

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

Sci_8633 @ 2024-01-12 22:06:04

#include<bits/stdc++.h>
using namespace std;
int x[105],y[105];
double sum;
double dis(double a,double b,double c,double d){
    double x;
    x=sqrt((c-a)*(c-a)+(d-b)*(d-b));
    return x;
}
int main(){
    for(int i=0;i<3;++i){
        cin>>x[i]>>y[i];
    }
    for(int i=0;i<2;++i)
        for(int j=i;j<2;++j)
            sum+=dis(x[j],y[j],x[j+1],y[j+1]);
    cout<<fixed<<setprecision(2)<<sum;
    return 0;
}

by LoadingSpace @ 2024-01-12 22:28:40

#include<bits/stdc++.h>
using namespace std;
double x[105],y[105];//要用double,否则80分
double sum;
double dis(double a,double b,double c,double d){
    double x;
    x=sqrt((c-a)*(c-a)+(d-b)*(d-b));
    return x;
}
int main(){
    for(int i=0;i<3;++i){
        cin>>x[i]>>y[i];
    }
    sum+=dis(x[0],y[0],x[1],y[1]);
    sum+=dis(x[1],y[1],x[2],y[2]);
    sum+=dis(x[0],y[0],x[2],y[2]);//只有三个,就不要搞花里胡哨的循环了,注意最后一个点要与第一个点测距
    printf("%.2f",sum);
    return 0;
}

by LoadingSpace @ 2024-01-12 22:30:18

还有最后用printf多好用啊,你非要用setprecision

()QwQ)


|