ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1084. Goat in the Garden

Precision problem... again
Posted by Vlad 25 Jan 2014 01:54
#include <iostream>
#include <fstream>
#include <cmath>
#define PI 3.14159265358979323846
using namespace std;

#ifndef ONLINE_JUDGE
    #define cin fin
    ifstream fin("input");
#endif

double N, R;

int main() {
    cin >> N >> R;
    N /= 2.0;

    if (R <= N) {
        cout << PI * R * R << '\n';
        return 0;
    }
    if (R >= N * sqrt(2.0)) {
        cout << 4.0 * N * N << '\n';
        return 0;
    }

    double A, B, C;
    A = N;
    C = R;
    B = sqrt(C * C - A * A);
    double angle = 90.0 - (acos(A / C) * 180.0 / PI) * 2.0;

    printf("%.3lf\n", 4.0 * (A * B + PI * R * R * angle / 360.0));
}

This program works fine on all the tests I saw on the forum. But I get WA #1 probably because of the wrong precision. I printed the value with 4 and 6 decimals but I get the same WA #1.
Could anyone tell me please how can I solve the "error" ? It's annoying to have a good solution and take WA.
Thanks a lot!


LATER EDIT:
It seems I succeeded with such a function:

inline void precise(double X, const int &decimals) {
    cout << (int)(X);
    cout << '.';
    X = X - (int)(X);
    for (int i = 1; i <= decimals; ++i) {
        X = X * 10.0;
        cout << (int)(X);
        X = X - (int)(X);
    }
    cout << '\n';
}

...

precise((double)(4.0 * (A * B + PI * R * R * angle / 360.0)), 3);

Have fun!

Edited by author 25.01.2014 04:19