I got puzzled when I was trying to solve the problem POJ1269-Intersecting Lines. Here is the link: http://poj.org/problem?id=1269 Here is my final AC code:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
int main(){
int n;
double x,y,xt,yt,xxt,yyt,xx,yy;
cin >> n;
printf("INTERSECTING LINES OUTPUT\n");
while(n --){
cin >> x >> y >> xt >> yt >> xx >> yy >> xxt >> yyt;
if((yt-y)*(xxt-xx) == (yyt-yy)*(xt-x)){
if((yt-y)*(xt-xx) == (yt-yy)*(xt-x)){
printf("LINE\n");
}
else{
printf("NONE\n");
}
}
else{
double X = ((x-xt)*(xx*yyt-xxt*yy) - (xx-xxt)*(x*yt-xt*y))
/ ((y-yt)*(xx-xxt) - (x-xt)*(yy-yyt));
double Y = ((y-yt)*(xx*yyt-xxt*yy) - (yy-yyt)*(x*yt-xt*y))
/ ((y-yt)*(xx-xxt) - (x-xt)*(yy-yyt));
printf("POINT %.2f %.2f\n", fabs(X) < 0.005 ? 0 : X,fabs(Y) < 0.005 ? 0 : Y);
}
}
printf("END OF OUTPUT\n");
return 0;
}
At the very beginning,I used "%.2lf"to output X and Y, but the result is Wrong Answer.I defined X and Y as double float point number, so I think I should use "%.2lf",but I got AC after I changed it to "%.2f". Why could it happen? Or where is wrong in my code?