功能:计算圆周率的近似值。

2026年09月10日 07:01
有1个网友回答
网友(1):

1. #include
2. #include
3. #include
4. using namespace std;
5.
6. int main()
7. {
8. const int MAX_TIMES = 200000000;
9. srand(static_cast(time(0)));
10.
11. int inside = 0;
12. for(int i = 0; i < MAX_TIMES; ++i) {
13. double x = static_cast(rand()) / RAND_MAX;
14. double y = static_cast(rand()) / RAND_MAX;
15. if(x * x + y * y <= 1.0) ++inside;
16. if(i % (MAX_TIMES / 100) == 0) cout << '.';
17. }
18.
19. double pi = 4.0 * inside / MAX_TIMES;
20. cout << "\nPI = " << pi << endl;
21. return 0;
22. }

======

http://blog.csdn.net/steedhorse/archive/2010/04/27/5532489.aspx

http://bbs.chinaunix.net/thread-696570-1-1.html