DDV1 @ 2024-08-02 20:06:21
样例没过却AC了的代码(C++)
#include<bits/stdc++.h>
using namespace std;
double dis0,dis1,dis2,dis3;
double dis(double x1,double y1,double x2,double y2){
double a,b,c;
a=pow((x2-x1),2);
b=pow((y2-y1),2);
c=sqrt(a+b);
return c;
}
int main(){
double x1,x2,x3,y1,y2,y3;
cin>>x1>>y1>>x2>>y2>>x3>>y3;
dis1=dis(x1,y1,x2,y2);
dis2=dis(x1,y1,x3,y3);
dis3=dis(x2,y2,x3,y3);
long double add;
add=dis1+dis2+dis3;
printf("%.2llf",add);
return 0;
}
。。。 dalao帮忙看看
不胜感激
by shiruoyu114514 @ 2024-08-02 20:11:26
@DDV1 long double的占位符是%Lf而不是%llf
by qazsedcrfvgyhnujijn @ 2024-08-02 20:12:30
把 long double add;
改成 double add;
,把最后一行 printf("$.2llf ..."
改成 printf(".2f", ...
就对了。
猜测第一个问题可能是隐式类型转换有关,第二个就纯纯语法问题
还有检查一下自己本地编译的 C++ 标准和洛谷提交用的是否一样
by wangjingxi_ @ 2024-08-02 20:13:43
#include <iostream>
#include <cmath>
using namespace std;
double dis(double x1, double y1, double x2, double y2) {
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}
int main() {
double x1, x2, x3, y1, y2, y3;
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
double dis1 = dis(x1, y1, x2, y2);
double dis2 = dis(x1, y1, x3, y3);
double dis3 = dis(x2, y2, x3, y3);
double add = dis1 + dis2 + dis3;
printf("%.2f", add);
return 0;
}
样例过了
by heyx0201 @ 2024-08-02 20:13:43
@DDV1
#include<bits/stdc++.h>
using namespace std;
double dis0,dis1,dis2,dis3;
double dis(double x1,double y1,double x2,double y2){
double a,b,c;
a=pow((x2-x1),2);
b=pow((y2-y1),2);
c=sqrt(a+b);
return c;
}
int main(){
double x1,x2,x3,y1,y2,y3;
cin>>x1>>y1>>x2>>y2>>x3>>y3;
dis1=dis(x1,y1,x2,y2);
dis2=dis(x1,y1,x3,y3);
dis3=dis(x2,y2,x3,y3);
double add;
add=dis1+dis2+dis3;
printf("%.2llf",add);
return 0;
}
这样能过样例,应该是 double
和 long double
之间转换的问题
by DDV1 @ 2024-08-02 21:07:07
谢谢@heyx0201@wangjingxi_ @qazsedcrfvgyhnujijn @shiruoyu (已关注)
by qazsedcrfvgyhnujijn @ 2024-08-02 21:19:59
哦对了,如果不想记 stdio
的神头鬼脸的占位符,可以用 cin/cout
,在 main
函数最前面加:
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
然后全程不混用 cin/cout
和 stdio
就好了,这样两者效率差不多
by Jason101 @ 2024-08-14 13:15:04
不用函数也能对耶
#include<bits/stdc++.h>
using namespace std;
int main()
{
double x1,y1,x2,y2,x3,y3;
cin >> x1 >> y1;
cin >> x2 >> y2;
cin >> x3 >> y3;
double l1 = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
double l2 = sqrt((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1));
double l3 = sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));
printf("%.2lf",l1 + l2 + l3);
return 0;
}