为什么s!=s1+s2+s3(应该是精度问题

P1355 神秘大三角

有没有神犇解释一下
by dbjbjbj @ 2022-10-12 21:03:40


1.5+1.5+1.5>4.5?
by dbjbjbj @ 2022-10-12 21:04:36


显然的,当你的点在三角形外时 `s>s1+s2+s3`
by reveal @ 2022-10-12 21:04:43


玄学~~精度~~90分 ```cpp #include<bits/stdc++.h> using namespace std; struct node{ int x,y; }a[4],b; inline int read() { int f = 1, x = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') { f = -1; } ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } return ~f ? x : -x; } inline void write(int x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) { write(x / 10); putchar(x % 10 + '0'); } else putchar(x + '0'); return; } double dis(node a,node b){ return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2)); } double S(node a,node b,node c){ double ab=dis(a,b),bc=dis(b,c),ac=dis(a,c); double p=(ab+bc+ac)/2; return sqrt(p*(p-ab)*(p-bc)*(p-ac)); } int main(){ // ios::sync_with_stdio(false); a[1].x=read(),a[1].y=read(); a[2].x=read(),a[2].y=read(); a[3].x=read(),a[3].y=read(); b.x=read(),b.y=read(); double s=S(a[1],a[2],a[3]),s1=S(a[1],a[2],b),s2=S(a[1],a[3],b),s3=S(a[2],a[3],b); // cout<<s<<' '<<s1<<' '<<s2<<' '<<s3; if(!s1||!s2||!s3){ if(s1==s||s2==s||s3==s){ putchar('4'); } else{ putchar('3'); } } else{ // cout<<s1+s2+s3; if(s1+s2+s3-s>0.0000001){ // cout<<s<<' '<<s1<<' '<<s2<<' '<<s3<<'\n'; putchar('2'); } else{ putchar('1'); } } // cin.tie(0); return 0; } ```
by dbjbjbj @ 2022-10-12 21:10:17


实际上,因为是整点多边形,可以使用叉积计算面积避免浮点数
by reveal @ 2022-10-12 21:17:53


自己输出多几位应该就会发现不是1.5,也不是4.5
by _YyD_ @ 2023-02-23 15:18:01


|