Weird logic error

Hello, today while I was solving a simple problem I encountered a logical error that idk why it occured, anyway here it is :


Using this input:

9
-565 -752
-184 723
-184 -752
-184 1
950 723
-565 723
950 -752
950 1
-565 1


This code has the logical error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <bits/stdc++.h>


#define fl(n)    for(int i = 0; i < n; i++)

#define Max(a,b) ( (a) > (b) ? (a) : (b))
#define Min(a,b) ( (a) < (b) ? (a) : (b))

#define ll   long long
#define nl   endl
#define pb push_back
#define mp make_pair


#define EPS  1e-9
#define INF  1e9
#define init -1e9


using namespace std;

struct coords{
int x;
int y;
}a[1000];

int main()
{

    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    cin >> a[i].x >> a[i].y;



    int k = 0;
    for(int i = 0; i < n; i++)
    {
        int c = 0;
        for(int j = 0; j < n; j++)
        {
            if(a[i].x == a[j].x && a[i].y > a[j].y)
            {
                c++;
            }
            if(a[i].x == a[j].x && a[i].y < a[j].y)
            {
                c++;
            }
            if(a[i].y == a[j].y && a[i].x < a[j].x)
            {
                c++;
            }
            if(a[i].y == a[j].y && a[i].x > a[j].x)
            {
                c++;
            }
        }
        if(c == 4) k++; //I tried >= aswell and it outputs either same or even larger numbers.
    }
    cout << k;


    return 0;
}


It outputs 9, correct answer is 1.



That's what I did to try and fix it and for some weird reason it worked:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <bits/stdc++.h>


#define fl(n)    for(int i = 0; i < n; i++)

#define Max(a,b) ( (a) > (b) ? (a) : (b))
#define Min(a,b) ( (a) < (b) ? (a) : (b))

#define ll   long long
#define nl   endl
#define pb push_back
#define mp make_pair


#define EPS  1e-9
#define INF  1e9
#define init -1e9


using namespace std;

struct coords{
int x;
int y;
}a[1000];

int main()
{

    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    cin >> a[i].x >> a[i].y;



    int k = 0;
    for(int i = 0; i < n; i++)
    {
        bool x = 0, b = 0, c = 0, d = 0;
        for(int j = 0; j < n; j++)
        {
            if(a[i].x == a[j].x && a[i].y > a[j].y)
            {
                x = 1;
            }
            if(a[i].x == a[j].x && a[i].y < a[j].y)
            {
                b = 1;
            }
            if(a[i].y == a[j].y && a[i].x < a[j].x)
            {
                c = 1;
            }
            if(a[i].y == a[j].y && a[i].x > a[j].x)
            {
                d = 1;
            }
        }
        if(x && b && c && d) k++;
    }
    cout << k;


    return 0;
}


Outputs 1.



I don't see a real difference between both methods but why did the first one output a wrong answer?

Here is the link to the question if you want to know what that input is although its not necessary: http://codeforces.com/problemset/problem/165/A
Last edited on
The first way you did it, each point has 4 other points that are either left, right, above or below it. You don't distinguish the direction, you just count them up and get 4. Because all 9 have this condition, you increment k for each point.

Your second method distinguishes between left, right, above and below. Because of this, only 1 point meets the criteria.

I don't see a real difference between both methods

What I don't understand is how you came up with the second method and not understand why it's different from the first. When you got the correct result, did you even evaluate what you had done. Did you step through your input file (or the samples from the linked web page) to see how they would be processed by your code? That's what a good programmer needs to learn to do. Step through your code and see how the variables change. When they change in an unexpected way, you need to revise your code.

Here is the link to the question if you want to know what that input is although its not necessary:

And no, the link you gave is very necessary. Without it I had no idea what you were trying to do.
Thanks for the reply doug, i was kind of lazy to trace it maybe lol cause I had other things so I thought asking here is a good idea :P but mb. thanks though!
Topic archived. No new replies allowed.