UVa 12575 – Sin Cos Problem

This one is a very interesting problem :).
In this problem I used atan2(a,b) library function, which returns the tan inverse value for all four quadrant on the other hand atan(a/b) returns the tan inverse value for 1st and 4th quadrant.

/*
User id: turing_13
Problem link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=4020
*/

#include <iostream>
#include <cstdio>
#include <cmath>
#define PI acos(-1)
using namespace std;

int main()
{
    int t;
    double a,b;
    cin>>t;
    while(t--)
    {
        cin>>a>>b;
        double angle=atan2(a,b);
        if(angle<0)
            angle+=(2*PI);
        printf("%.2lf %.2lf\n",angle,sqrt(a*a+b*b));
    }
    return 0;
}

And another solution is here…


#include <iostream>
#include <cstdio>
#include <cmath>
#define PI acos(-1)
using namespace std;

int main()
{
    int t;
    double a,b;
    cin>>t;
    while(t--)
    {
        cin>>a>>b;
        if(a==0 && b==0)
        {
            cout<<"0.00 0.00\n";
            continue;
        }
        double angle=atan(a/b);
        double ans=sqrt(a*a+b*b);
        for(; angle<=2*PI; angle+=(PI/2))
            if(abs(a*sin(angle)+b*cos(angle)-ans)< 0.01 && angle>=0)
                break;
        printf("%.2lf %.2lf\n",angle,ans);
    }
    return 0;
}


Advertisements