hoj 1408 欧几里得算法

hoj  1408

Pi

My Tags  (Edit)
  Source : ACM ICPC East Central North America 1995
  Time limit : 1 sec   Memory limit : 32 M

Submitted : 654, Accepted : 371

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't have 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 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.

Sample Input

5
2
3
4
5
6
2
13
39
0

Sample Output

3.162278
No estimate for this data set.
题目大意: 给你一组数,然后这组数两两组合,数不能相同  如果两个数互质(最大公约数是1) 然后利用 所给公式估计 pi 的值 

解决方法:

                第一步:  数据结构,输入 用数组进行存储;

                第二步: 算法 , 两两组合 , 用两重循环实现, 分别判断是否互质 ,并记录互质的组合数

                第三步:输出结果,题目比较简单,就不多说了

细节问题: 开始一定注意是用 double 数据类型 否则不会被ac 的 而且 sum 主观上应该是整数 但是也要定义成double  因为他要除以一个浮点数 这好似十分重要的

源代码:

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

int oulid(int a, int b)
{
    if(b == 0)
    {
        return a;
    }
    int t = a%b;
    while(t != 0)
    {
        a = b;
        b = t;
        t = a%b;
    }
    return  b;
}
int main()
{
    int  a[60];
    int N ;
    int sum , z;

    while(scanf("%d", &N) != EOF)
    {
        int i, j;
        int  t = 0;
        if(N == 0)
        {
            return 0;
        }
        for(i = 0; i < N ; i++)
        {
            scanf("%d", &a[i]);
        }
        double  sum = N*(N-1)/2;
        for(i = 0; i < N ;i ++)
        {
            for(j = i+1; j < N; j++)
            {
                 z = oulid(a[i], a[j]);
                 if( z == 1)
                 {
                     t ++;
                 }
            }
        }
        if(t == 0 )
        {
           printf("No estimate for this data set.\n");

        }
        else
        {
            double e = sqrt(6*sum/t);
            printf("%.6lf\n", e);
        }

    }
    return 0;
}


wangxiaoming CSDN认证博客专家 架构 Spring Boot Redis
博客是很好的总结和记录工具,如果有问题,来不及回复,关注微信公众号:程序员开发者社区,获取我的联系方式,向我提问,也可以给我发送邮件,联系 1275801617@qq.com
03-04
Professor Robert A. J. Matthews of the Applied Mathematics and Computer Science Department at the University of Aston in Birmingham, England has recently described how the positions of stars across the night sky may be used to deduce a surprisingly accurate value of Pi. This result followed from the application of certain theorems in number theory. Here, we don't have the night sky, but can use the same theoretical basis to form an estimate for Pi: Given any pair of whole numbers chosen from a large, random collection of numbers, the probability that the two numbers have no common factor other than one (1) is 6/Pi^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/Pi^2 = 6/10 Pi = 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 no common factor other than one (1), and use the method illustrated above to obtain an estimate for Pi. 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 Pi 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 Pi. 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. Sample Input 5 2 3 4 5 6 2 13 39 0 Sample Output 3.162278 No estimate for this data set.
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页
实付 19.89元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值