zoukankan      html  css  js  c++  java
  • ZOJ Problem Set–1337 Pi

    Time Limit: 2 Seconds      Memory Limit: 65536 KB


    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.


    Source: East Central North America 1995

    仔细阅读该题目的描述以后,发现其实该题的核心就是最大公约数的计算问题。代码如下:

    #include<iostream>
    
    #include<vector>
    
    #include<iomanip>
    
    #include<math.h>
    
    using namespace std;
    
    /*
    
    * 计算最大公因数的函数,干函数来源于博客园网友的分享
    
    * 地址是:http://www.cnblogs.com/drizzlecrj/archive/2007/09/14/892340.html
    
    */
    
    int gcd(int a, int b)
    
    {
    
      if(a == 0) return b;
    
        if(b == 0) return a;
    
        if(a % 2 == 0 && b % 2 == 0) return 2 * gcd(a >> 1, b >> 1);
    
        else if(a % 2 == 0)  return gcd(a >> 1, b);
    
        else if(b % 2 == 0) return gcd(a, b >> 1);
    
        else return gcd(a - b > 0 ? a - b : b-a, a > b ? b:a);
    
    }
    
    int main()
    
    {
    
      int numbers;
    
      while(cin>>numbers && numbers)
    
      {
    
        vector<int> numCollection;
    
        int numCount;
    
        int minFactor = 0;
    
        int totalParis = 0;
    
        for(int i = 0;i < numbers;i++)
    
        {
    
          cin>>numCount;
    
          for(int j = 0; j < numCollection.size(); j++)
    
          {
    
            totalParis++;
    
            if(gcd(numCount, numCollection[j]) == 1)
    
            {
    
              minFactor++;
    
            }
    
          }
    
          numCollection.push_back(numCount);
    
        }
    
        if(minFactor == 0)
    
        {
    
          cout<<"No estimate for this data set."<<endl;
    
        }
    
        else
    
        {
    
          cout<<fixed<<setprecision(6)<<sqrt(6.0*totalParis/minFactor)<<endl;
    
        }
    
      }
    
      return 0;
    
    }
  • 相关阅读:
    ios testing
    Visual Studio文件自动定位功能
    如何:为 SharePoint 2013 中的 PerformancePoint Services
    sharepoint application page
    power rename batch files
    sharepoint app development configuration
    Installing and Configuring Workflow Manager 1.0
    安装用于 SharePoint 2013 的 Reporting Services SharePoint 模式
    ios dev tech
    Applications command line
  • 原文地址:https://www.cnblogs.com/malloc/p/2396587.html
Copyright © 2011-2022 走看看