ZOJ 1337 Pi
来源:互联网 发布:什么软件可以哼歌识曲 编辑:程序博客网 时间:2024/04/26 07:43
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.
题意:输入一组数据,他们两两按顺序组成序列,总共有n(n-1)/2对,然后找出最大公约数为1的序列有几对,
然后利用上述公式,计算Pi
代码:
#include <stdio.h>
#include <math.h>
int gcd(int a,int b)
{
if(a>=b)
{
while(b!=0)
{
int temp=a%b;
a=b;
b=temp;
}
return a;
}
else
{
while(a!=0)
{
int temp=b%a;
b=a;
a=temp;
}
return b;
}
}
int main()
{
int N;
int i,j;
int a[100];
while(scanf("%d",&N))
{
if(N==0)
break;
for(i=0;i<N;i++)
scanf("%d",&a[i]);
double sum=N*(N-1)/2;
int t=0;
for(i=0;i<N;i++)
{
for(j=i+1;j<N;j++)
{
if(gcd(a[i],a[j])==1)
t++;
}
}
if(t==0)
printf("No estimate for this data set.\n");
else
{
double d=sqrt(6*sum/t);
printf("%.6f\n",d);
}
}
}
- ZOJ 1337 Pi
- zoj 1337 pi
- zoj 1337 Pi
- ZOJ 1337 Pi
- zoj 1337 Pi
- ZOJ 1337 Pi
- zoj 1337 Pi
- PI
- pi
- Pi
- PI
- pi
- Pi
- Pi
- [pi
- pi
- PI
- zoj 1337
- ListView滚动位置的记录回复
- AndroidManifest.xml 之 android.intent.action.MAIN & android.intent.category.LAUNCHER
- RTP协议分析
- mysql 自动执行的脚本
- “移动硬盘不显示盘符,磁盘管理中能找到..."的解决办法
- ZOJ 1337 Pi
- 大话Windows 8:或“唤醒”Linux?
- 直接插入排序算法
- 变量的定义,初始化和声明
- oracle 默认值需要注意的一个地方
- Java JNI(Java Native Interface)
- android之内存管理的文章收集
- 高效能创业者的七项习惯
- {A} + {B}