SPOJ- TWO CIRCLES

closed account (1vf9z8AR)
https://www.spoj.com/problems/SMPCIRC/

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
  #include<iostream>
#include<math.h>
using std::cout;
using std::cin;
int main()
{
    int t;
    cin>>t;
    double x1,y1,r1,x2,y2,r2;
    while(t>0)
    {
        cin>>x1>>y1>>r1>>x2>>y2>>r2;
        double dist=(sqrt(pow((x1-x2),2)+pow((y1-y2),2)));
        if(dist==(r2-r1))
            cout<<"E";
        else if(dist<(r2-r1))
            cout<<"I";
        else
            cout<<"O";
        if(t!=1)
                cout<<std::endl;
        t-=1;
    }
}


Even after the right output, I get wrong answer?
Might be a problem with comparison of floating point numbers.
Why don't you print the vars and result and show us what you got.
closed account (1vf9z8AR)
Ok, I avoided float this time.

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
#include<iostream>
#include<math.h>
using std::cout;
using std::cin;
int main()
{
    int t;
    cin>>t;
    double x1,y1,r1,x2,y2,r2;
    while(t>0)
    {
        cin>>x1>>y1>>r1>>x2>>y2>>r2;
        int sqdist=(((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)));
        if(r2<r1)
        {
            cout<<"O";
           if(t!=1)
                cout<<std::endl;
                t-=1;
            continue;
        }
        else
        {
        if(sqdist==(r2-r1)*(r2-r1))
            cout<<"E";
        else if(sqdist<(r2-r1)*(r2-r1))
            cout<<"I";
        else
            cout<<"O";
        }
        if(t!=1)
                cout<<std::endl;
        t-=1;
    }
}


But still the wrong answer?
Sorry for not being more precise. The operator == doesn't work for float and doubles due to rounding errors. It's very sad the books and tutorials don't mention it. :(

https://stackoverflow.com/questions/18971533/c-comparison-of-two-double-values-not-working-properly?lq=1
closed account (1vf9z8AR)
Ok, now I avoid double and float. But I am still getting the wrong answer.

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
#include<iostream>
#include<math.h>
using std::cout;
using std::cin;
int main()
{
    int t;
    cin>>t;
    int x1,y1,r1,x2,y2,r2;
    while(t>0)
    {
        cin>>x1>>y1>>r1>>x2>>y2>>r2;
        int sqdist=(((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)));
        if(r2<r1)
        {
            cout<<"O";
           if(t!=1)
                cout<<std::endl;
                t-=1;
            continue;
        }
        else
        {
        if(sqdist==(r2-r1)*(r2-r1))
            cout<<"E";
        else if(sqdist<(r2-r1)*(r2-r1))
            cout<<"I";
        else
            cout<<"O";
        }
        if(t!=1)
                cout<<std::endl;
        t-=1;
    }
}
Last edited on
closed account (1vf9z8AR)
Ok, problem solved!!!

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
#include<iostream>
#include<math.h>
using std::cout;
using std::cin;
int main()
{
    int t;
    cin>>t;
    double x1,y1,r1,x2,y2,r2;
    while(t>0)
    {
        cin>>x1>>y1>>r1>>x2>>y2>>r2;
        int sqdist=(((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)));
        if(sqdist==(r2-r1)*(r2-r1))
            cout<<"E";
        else if(sqdist<(r2-r1)*(r2-r1))
            cout<<"I";
        else
            cout<<"O";
        if(t!=1)
                cout<<std::endl;
        t-=1;
    }
}

Topic archived. No new replies allowed.