UVA412 LA5378 POJ1491 ZOJ1337 Pi【模拟+GCD】

Professor Robert A. J. Matthews of the Applied Mathematics and Computer Science Department at the University of Aston in Birmingham, England has recently described howthe positions of stars across the night skymay be used to deduce a surprisingly accurate value of π. This result followed from the application of certain theorems in number theory.
    Here, we don’thave the night sky, but can use the same theoretical basis to form an estimate for π:
    Given any pair of whole numbers chosen from a large, random collection of numbers, the probability that the twonumbers have no common factor other than one (1) is
6/π2
    For example, using the small collection of numbers: 2, 3, 4, 5, 6; there are 10 pairs that can be formed: (2,3), (2,4), etc. Six of the 10 pairs: (2,3), (2,5), (3,4), (3,5), (4,5) and (5,6) have no common factor other than one. Using the ratio of the counts as the probability we have:
6/π2≈6/10
π ≈ 3.162
    In this problem, you’ll receive a series of data sets. Each data set contains a set of pseudo-random positive integers. For each data set, find the portion of the pairs which may be formed that have nocommon factor other than one (1), and use the method illustrated above to obtain an estimate for π. Report this estimate for each data set.
Input
The input consists of a series of data sets.
    The first line of each data set contains a positive integer value, N, greater than one (1) and less than 50.
    There is one positive integer per line for the next N lines that constitute the set for which the pairs are to be examined. These integers are each greater than 0 and less than 32768.
    Each integer of the input stream has its first digit as the first character on the input line.
    The set size designator, N, will be zero to indicate the end of data.
Output
A line with a single real value is to be emitted for each input data set encountered. This value is the estimate for π for the data set. An output format like the sample below should be used. Answers must be rounded to six digits after the decimal point.
    For some data sets, it may be impossible to estimate a value for π. This occurs when there are no pairs without common factors. In these cases, emit the single-line message:
No estimate for this data set.
exactly, starting with the first character, ‘N’, as the first character on the line.
Note: Your output for the float/real, using your chosen language, may be default-formatted differently.
Sample Input
5
2
3
4
5
6
2
13
39
0
Sample Output
3.162278
No estimate for this data set.

East Central North America 1995

问题链接UVA412 LA5378 POJ1491 ZOJ1337 Pi
问题简述:给定一种估算Pi的方法,即给出一系列随机数,从中任选两个数,这两个数的最大公约数不大于1(互质)的概率为6/(Pi*Pi),然后给出一系列数,据此估算Pi的值。
问题分析:直接模拟计算。
程序说明:使用库函数__gcd()计算最大公约数。
参考链接:(略)
题记:能用库函数要用库函数。

AC的C++语言程序如下:

/* UVA412 Pi  */

#include <bits/stdc++.h>

using namespace std;

const int N = 50 + 1;
int a[N];

int main()
{
    int n;
    while(~scanf("%d", &n) && n) {
        int cnt = 0;
        for(int i = 0; i < n; i++) scanf("%d", &a[i]);

        for(int i = 0; i < n; i++)
            for(int j = i + 1; j < n; j++)
                if(__gcd(a[i], a[j]) == 1) cnt++;

        if (cnt) printf("%.6f\n", sqrt(3.0 * n * (n - 1) / cnt));
        else puts("No estimate for this data set.");
    }

    return 0;
}
原创文章 2323 获赞 2382 访问量 269万+

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/105772009