想问一下下为什么80分

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

zlh202483023 @ 2024-12-04 16:32:50

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double dis(int a, int b, int c, int d);
int main() {
    int x1, x2, y1, y2, x3, y3;
    float a, b, c, sum;
    cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
    a = dis(x1, y1, x2, y2);
    b = dis(x1, y1, x3, y3);
    c = dis(x2, y2, x3, y3);
    sum = a + b + c;
    cout << fixed << setprecision(2) << sum;
}

double dis(int a, int b, int c, int d) {
    float distant;
    distant = sqrt((a - c) * (a - c) + (b - d) * (b - d));
    return distant;
}

by Diary_Of_Young @ 2024-12-04 16:40:15

@zlh202483023 把所有变量类型全换成double,精度估计有问题。


by Diary_Of_Young @ 2024-12-04 16:40:45

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double dis(double a, double b, double c, double d);
int main() {
    double x1, x2, y1, y2, x3, y3;
    double a, b, c, sum;
    cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
    a = dis(x1, y1, x2, y2);
    b = dis(x1, y1, x3, y3);
    c = dis(x2, y2, x3, y3);
    sum = a + b + c;
    cout << fixed << setprecision(2) << sum;
}

double dis(double a, double b, double c, double d) {
    double distant;
    distant = sqrt((double)(a - c) * (a - c) + (double)(b - d) * (b - d));
    return distant;
}

by zlh202483023 @ 2024-12-05 10:18:12

@Diary_Of_Young谢谢!


|