C++,全WA,样例过了,求助大佬

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

Romanticism_null @ 2020-08-03 17:00:53

还有个疑惑,之前变量名y用的y1,不能用。

以及输出直接输出的dis,样例没过,加了个sum过了。

蒟蒻瑟瑟发抖

#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int x1,x2,x3,y,y2,y3;
double sum; 
double dis;
int juli(int n)
{
    dis=sqrt((x2-x1)*(x2-x1)+(y2-y)*(y2-y))+sqrt((x3-x1)*(x3-x1)+(y3-y)*(y3-y))+sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
    return dis;
}
int main()
{
    cin>>x1>>y>>x2>>y2>>x3>>y3;
    for(int i=0;i<3;i++)
    sum=juli(dis);
    printf("%.2lf",sum);
    return 0;
}

by shao_qian @ 2020-08-03 17:05:14

@jsdhwdmaX_zh

  1. y1不能用是因为cmath库里面有一个函数也叫y1.

  2. juli函数返回double啊啊


by shao_qian @ 2020-08-03 17:05:55

@jsdhwdmaX_zh 另外juli这个函数写得奇奇怪怪


by Romanticism_null @ 2020-08-03 17:06:40

改了一下,最后一个检查点WA了

#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int x1,x2,x3,y,y2,y3;
double sum; 
double dis;
double juli(double n)
{
    dis=sqrt((x2-x1)*(x2-x1)+(y2-y)*(y2-y))+sqrt((x3-x1)*(x3-x1)+(y3-y)*(y3-y))+sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
    return dis;
}
int main()
{
    cin>>x1>>y>>x2>>y2>>x3>>y3;
    for(int i=0;i<3;i++)
    sum=juli(dis);
    printf("%.2lf",sum);
    return 0;
}

by fdc99 @ 2020-08-03 17:07:57

e把那个函数返回值改成double即可,dis最好*1.0,y1的话应该可以使用(之前我用y1变量就过了)


by shao_qian @ 2020-08-03 17:08:26

@jsdhwdmaX_zh

double dis(int x1,int y1,int x2,int y2){
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
……
ans=dis(_x1,_y1,_x2,_y2)+dis(_x2,_y2,_x3,_y3)+dis(_x3,_y3,_x1,_y1);

by shao_qian @ 2020-08-03 17:09:35

@shao_qian e...那个y1就自己改一下了


by Jiangjinggao @ 2023-05-27 09:02:29

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

float power(float a, float b) {
    return (a - b) * (a - b);
}

float dist(float x1, float y1, float x2, float y2) {
    return sqrt(power(x1, x2) + power(y1, y2));
}

int main() {
    float p[3][2], ans = 0;
    for (int i = 0; i < 3; i++) {
        scanf("%f%f", &p[i][0], &p[i][1]);
    }

    ans += dist(p[0][0], p[0][1], p[1][0], p[1][1]);
    ans += dist(p[1][0], p[1][1], p[2][0], p[2][1]);
    ans += dist(p[0][0], p[0][1], p[2][0], p[2][1]);

    printf("%.2f", ans);

    return 0;
}

|