关闭

hoj 1408 欧几里得算法

559人阅读 评论(0) 收藏 举报
分类:

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;
}


0
0
查看评论

TJU 1100 Pi double精度 gcd

1100.   Pi Time Limit: 1.0 Seconds   Memory Limit: 65536K Total Runs: 6001   Accepted Runs: 24...
  • qq_32454729
  • qq_32454729
  • 2016-08-19 01:40
  • 151

ZOJ 1337 Pi

Pi Time Limit: 2 Seconds      Memory Limit: 65536 KB Professor Robert A. J. Matthews of the Applied Mathemati...
  • wpfengqi
  • wpfengqi
  • 2012-08-24 10:31
  • 291

LightOJ 1408 Batting Practice (期望)

题目连接:http://lightoj.com/volume_showproblem.php?problem=1408 题意:连续进k1个球或连续不进k2个球则游戏结束,给出进球概率p,求到游戏结束时投球个数的期望。 思路:f[i]表示连续i次不命中时到游戏结束剩余投球个数的期望,t[i]表示连...
  • whyorwhnt
  • whyorwhnt
  • 2013-08-11 20:13
  • 978

LightOJ-1408 Batting Practice(期望&推公式)

Q - Batting Practice  LightOJ - 1408  题意:每次投球投偏的概率为p,如果连续投中k1次或者连续投偏k2次则投球结束,问投球个数的期望值 题解:数学期望推导公式 设f(x)为连续投偏x次的期望,g(x...
  • qq_31759205
  • qq_31759205
  • 2017-01-23 17:14
  • 174

贪心法 hoj1003

#include #include using namespace std; pairmilk[5001]; int main() {int m,n,i,j,k,minnum,minmoney,need; while(cin>>n>>m) { minnum=0;k=0;m...
  • u013497977
  • u013497977
  • 2014-03-23 15:41
  • 442

HOJ 2662 Pieces Assignment(状态压缩DP)

题目特征: 1.求方法数目,告诉一堆约束条件(相邻,不攻击之类),其中有个数一般是 状态转移: 1.跟要相互影响的行有关,跟状态有关,跟给出条件有关(题目要求要达到一定数目等,但是不是具体说明一般不用,看看其他的) 2.一般题目情景为线是二维,面为三维 代码: 1.判断约束条件 2.记录状态中1的个...
  • booyoungxu
  • booyoungxu
  • 2015-07-23 11:15
  • 1050

LightOJ1408---Batting Practice (期望,推公式)

After being all out for 58 and 78 in two matches in the most prestigious tournament in the world, the coach of a certain national cricket team was ver...
  • Guard_Mine
  • Guard_Mine
  • 2015-05-26 20:09
  • 854

算法-欧几里得算法(C++实现)

欧几里得算法求得的是两个整数的最大公因式。yin
  • chenfs1992
  • chenfs1992
  • 2014-07-24 09:41
  • 1147

杭电1408--精度问题

盐水的故事 Problem Description 挂盐水的时候,如果滴起来有规律,先是滴一滴,停一下;然后滴二滴,停一下;再滴三滴,停一下…,现在有一个问题:这瓶盐水一共有VUL毫升,每一滴是D毫升,每一滴的速度是一秒(假设最后一滴不到D毫升,则花费的时间也算一秒),停一下的时间也是一秒这瓶水什...
  • qq_30638831
  • qq_30638831
  • 2015-11-06 21:28
  • 234

POJ1408-Fishnet

转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1302523813  大致题意: 一个1X1的正方形,每条边上有n个不同的点(不包括顶点),并给出它们的坐标。现在把对边相对应的点相连,将正方形分割成(n+1)*(n+
  • lyy289065406
  • lyy289065406
  • 2011-07-31 17:01
  • 2280
    个人资料
    • 访问:268361次
    • 积分:5874
    • 等级:
    • 排名:第5199名
    • 原创:341篇
    • 转载:13篇
    • 译文:0篇
    • 评论:28条
    感谢打赏 支付宝二维码
    支付宝
    微信公众号 欢迎关注
    微信
    发福利啦 支付宝扫码有红包
    支付宝
    最新评论