0

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?

  • 3
    what is AC ? Why don't you compile and run the code locally to see what the output is in both cases and how it differs? Relying on a black box for testing and debugging can be done, but is unnecessarily complicated. – 463035818_is_not_an_ai Dec 18 '20 at 13:52
  • https://stackoverflow.com/questions/7197589/c-what-is-the-printf-format-spec-for-float – Retired Ninja Dec 18 '20 at 13:55
  • 1
    Also, after you know the values of x & y you care about ... none of your code is part of the [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) _apart_ from the `printf`. Right? We don't have your input, so none of the other code is at all helpful in reproducing the problem. – Useless Dec 18 '20 at 13:55
  • 1
    `%.2lf` should work. Are you sure you didn't have `%.2Lf`? – Eljay Dec 18 '20 at 13:58
  • 1
    [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Gary Strivin' Dec 18 '20 at 14:22

1 Answers1

1

I defined X and Y as double float point number, so I think I should use "%.2lf"

Depends on language version. The C standard library functions inherited by C++03 and older are from C89 in which using %lf format specifier results in undefined behaviour.

Since C++11 which inherits the C99 standard, the use of %lf is allowed with printf and the behaviour is identical to using %f.

eerorika
  • 232,697
  • 12
  • 197
  • 326