UVa 412

  题目大意:给定一种估算Pi的方法:给出一系列随机数,从中任选两个数,这两个数的最大公约数不大于1(互质)的概率为6/(Pi*Pi),然后给出一系列数,据此估算Pi的值。直接模拟就好了。

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 int gcd(int a, int b)
 7 {
 8     return b == 0 ? a : gcd(b, a%b);
 9 }
10 
11 int main()
12 {
13 #ifdef LOCAL
14     freopen("in", "r", stdin);
15 #endif
16     int n;
17     int a[60];
18     while (scanf("%d", &n) && n)
19     {
20         for (int i = 0; i < n; i++)
21             scanf("%d", &a[i]);
22         int cnt = 0;
23         for (int i = 0; i < n-1; i++)
24             for (int j = i+1; j < n; j++)
25             {
26                 int lmax = max(a[i], a[j]);
27                 int lmin = min(a[i], a[j]);
28                 if (gcd(lmax, lmin) == 1)
29                     cnt++;
30             }
31         if (cnt == 0)
32         {
33             printf("No estimate for this data set.
");
34             continue;
35         }
36         int total = n*(n-1)/2;
37         printf("%.6lf
", sqrt(6.0*total/cnt));
38     }
39     return 0;
40 }
View Code
原文地址:https://www.cnblogs.com/xiaobaibuhei/p/3308066.html