YuuKiErii @ 2022-06-19 08:58:12
代码如下:
#include<bits/stdc++.h>
using namespace std;
double dis(double x1, double y1,double x2,double y2){
double a;
a=sqrt(abs((x2-x1))*abs((x2-x1))+abs((y2-y1))*abs((y2-y1)));
return a;
}
int main (){
int x1,y1,x2,y2,x3,y3;
cin>>x1>>y1>>x2>>y2>>x3>>y3;
double c;
int dis1=dis(x1,y1,x2,y2);
int dis2=dis(x2,y2,x3,y3);
int dis3=dis(x1,y1,x3,y3);
c=dis1+dis2+dis3;
printf("%.2lf",c);
return 0;
} ```
by XKqwq @ 2022-06-19 08:59:14
@zhangzihan23333 先改变量名y1
by YuuKiErii @ 2022-06-19 09:01:01
@jacobigSB y1不行吗,这个是不是和一些东西冲突了?
by z_yk @ 2022-07-04 09:17:06
第12,13,14行的不能用int类型存储要不的话那个c中只会有整数而不是双精度的浮点数(其实也是,但是遇到浮点类型的肯定错) 测试点一: 输入: 0 0 -1 2 -2 2 输出: 6.06 改过的代码:
#include<bits/stdc++.h>
using namespace std;
double dis(double x1, double y1,double x2,double y2){
double a;
a=sqrt(abs((x2-x1))*abs((x2-x1))+abs((y2-y1))*abs((y2-y1)));
return a;
}
int main (){
int x1,y1,x2,y2,x3,y3;
cin>>x1>>y1>>x2>>y2>>x3>>y3;
double c;
double dis1=dis(x1,y1,x2,y2);
double dis2=dis(x2,y2,x3,y3);
double dis3=dis(x1,y1,x3,y3);
c=dis1+dis2+dis3;
printf("%.2lf",c);
return 0;
}
我的代码:
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
double x[4],y[4];
for(int i=1;i<=3;i++)
{
cin>>x[i]>>y[i];
}
double a=sqrt((x[2]-x[1])*(x[2]-x[1])+(y[2]-y[1])*(y[2]-y[1]));
double b=sqrt((x[3]-x[1])*(x[3]-x[1])+(y[3]-y[1])*(y[3]-y[1]));
double c=sqrt((x[3]-x[2])*(x[3]-x[2])+(y[3]-y[2])*(y[3]-y[2]));
printf("%.2f",a+b+c);
return 0;
}
by z_yk @ 2022-07-04 09:22:03
还有一个问题,最后一个测试点的输入数据都是浮点数,这是一个坑。
最后一个测试点
输入:
23.234 12.123
-99.99 99.99
-1 -100
输出:
489.20
所以我又改了一下
#include<bits/stdc++.h>
using namespace std;
double dis(double x1, double y1,double x2,double y2){
double a;
a=sqrt(abs((x2-x1))*abs((x2-x1))+abs((y2-y1))*abs((y2-y1)));
return a;
}
int main (){
double x1,y1,x2,y2,x3,y3;//改的这里
cin>>x1>>y1>>x2>>y2>>x3>>y3;
double c;
double dis1=dis(x1,y1,x2,y2);
double dis2=dis(x2,y2,x3,y3);
double dis3=dis(x1,y1,x3,y3);
c=dis1+dis2+dis3;
printf("%.2lf",c);
return 0;
}
这样应该能通过