简体   繁体   English

C ++中struct的奇怪行为

[英]Strange behaviour of struct in C++

I was solving problem 10263 (Railway) of uva online judge ( http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1204 ) and getting Wrong Answer for my code. 我正在解决uva在线裁判( http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1204 )的问题10263(铁路),并且得到了我的代码错误的答案。 After discussing it with a friend (who also solved the problem and got Accepted) he took a look at my code and it was extremely similar to his (all the geometry functions were the same, for example, since we are training as a team for ICPC and using the same code libraries). 与朋友(也解决了问题并被接受)讨论之后,他看了看我的代码,它与他的代码极其相似(例如,所有几何函数都相同,因为我们正在接受团队培训与ICPC并使用相同的代码库)。

So, after I started trying one change at a time to see what the problem was, I found this strange behaviour. 因此,在我开始尝试一次更改以查看问题所在之后,我发现了这种奇怪的行为。

My initial code (which got Wrong Answer) is this one: 我的初始代码(得到错误答案)是这样的:

#include <iostream>
#include <cmath>
#include <cstdio>
#include <limits>
#define EPS 1e-9
using namespace std;

struct point {
    double x, y;
    point(double _x, double _y) { x = _x, y = _y; } //EDITED
    point() { x = 0.0, y = 0.0; }
};

double dist(point p1, point p2) {
    return hypot(p1.x - p2.x, p1.y - p2.y); 
}

double distToLine(point p, point A, point B, point *c) {
    double scale = (double) ((p.x - A.x) * (B.x - A.x) + (p.y - A.y) * (B.y - A.y)) / ((B.x - A.x) * (B.x - A.x) + (B.y - A.y) * (B.y - A.y));
    c->x = A.x + scale * (B.x - A.x);
    c->y = A.y + scale * (B.y - A.y);
    return dist(p, *c); 
}

double distToLineSegment(point p, point A, point B, point* c) {
    if ((B.x-A.x) * (p.x-A.x) + (B.y-A.y) * (p.y-A.y) < EPS) {
        c->x = A.x; c->y = A.y;
        return dist(p, A); 
    }
    if ((A.x-B.x) * (p.x-B.x) + (A.y-B.y) * (p.y-B.y) < EPS) {
        c->x = B.x; c->y = B.y; 
        return dist(p, B); 
    } 
    return distToLine(p, A, B, c); 
}

int main() {
    int Mx, My;
    while (cin >> Mx) {
        int N, x, y;
        cin >> My >> N;

        point A, B, aux, res, M(Mx, My);
        double dres = numeric_limits<double>::infinity(), d;

        cin >> x >> y;
        A = point(x,y);

        for (int i=0; i<N; i++) {
            cin >> x >> y;
            B = point(x,y);

            d = distToLineSegment(M, A, B, &aux);
            if (d < dres) {
                dres = d;
                res = aux;
            }
            A = B;
        }


        printf("%.4f\n%.4f\n",res.x,res.y);
    }
}

Now, after changing this lines: 现在,更改此行之后:

int Mx, My;
    while (cin >> Mx) {
        int N, x, y;
        cin >> My >> N;

        point A, B, aux, res, M(Mx, My);

To

point M;
    while (cin >> M.x) {
        int N, x, y;
        cin >> M.y >> N;

        point A, B, aux, res;

My solution got accepted. 我的解决方案被接受了。 Could anyone please help me understand what was the issue with reading the int values and then creating the point? 任何人都可以帮助我理解读取int值然后创建点的问题吗? In what possible causes could this code "misbehave" and generate different answers than the other one? 在何种可能的原因下,此代码“行为不当”并产生与另一个不同的答案? Also, any ideas on why "M" was the only problem? 另外,关于“ M”为何是唯一问题的任何想法? (I kept reading "A" and "B" the same way as before and it didn't affect the answer, only the way I was reading "M" had to be changed). (我一直以与以前相同的方式读取“ A”和“ B”,但这并没有影响答案,只是必须更改我读取“ M”的方式)。

Any hints to help me understand what is going on would be much appreciated! 任何帮助我了解发生了什么的提示将不胜感激!


Edit: Wrote the constructor wrong when "changing it back to the wrong version", sorry. 编辑:“将其更改回错误的版本”时,将构造函数写错了,抱歉。

The difference is wich @jhonchen902 suggest in the comment: The problem specifies output must be floating-point numbers with four digits after the decimal point. 区别在于注释中的@ jhonchen902:问题指定输出必须是小数点后四位数的浮点数。

In your first version, you where using integers. 在您的第一个版本中,您使用整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM