Tuesday, October 15, 2013

uri online judge 1002 solution (finally :)

yes i solved it eventually after many attemps

you need to know that

Number of digits after the decimal point

needs to use double, because

Wrong Answer in simple problem using floating-point

Try to use double instaed float in C++. This is a common error due the precision of each one of them.

here is the uri online judge 1002 problem: http://www.urionlinejudge.com.br/judge/en/problems/view/1002

Area of a Circle

The area of the circle is defined as A = π . R2, having π as 3.14159.
Calculate the area using the formula given in the problem description.

Input

Read the variable R, that is the radius of the circle.

Output

Print the variable A, rounded to four decimal digits.
Sample InputSample Output
2A=12.5664
and here is my solution in c++: http://ideone.com/lQPoPh
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. int main() {
  5. double A, p = 3.14159, R;
  6. cin >> R;
  7. A = p * R * R;
  8. cout << fixed << setprecision(4);
  9. cout << "A=" << A << endl;
  10. return 0;
  11. }

and here is another solution in c++: http://ideone.com/hiebng

#include <iostream>
#define pi 3.14159
using namespace std;
long double r,area;
int main(){
    cin>>r;     area=(r*r)*pi;
    cout.setf(ios::fixed,ios::floatfield);
    cout.precision(4);
    cout <<"A=" << area;
    return 0;
}


(the above shown solution belongs to Quan Tran.)

No comments:

Post a Comment