注:

  1. 对于double计算,一定要小心,必要时把与double计算相关的所有都变成double型。

  2. for (int i = 0; i < N; i++)         //N 不可写为N - 1,否则当N为1时无法进行;

原题:

1100.   Pi


Time Limit: 1.0 Seconds   Memory Limit: 65536K
Total Runs: 5683   Accepted Runs: 2317

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 π. 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 two numbers have no common factor other than
one (1) is

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:

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 π. 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.

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 <iomanip>
#include <cmath>
#include <stdio.h>
using namespace std; int num[]; int gcd(int a, int b) {
return b == ? a : gcd(b, a % b);
} int main()
{
int N;
while (cin >> N && N != ) {
int count = ; double sum = ;
for (int i = ; i < N; i++) cin >> num[i];
for (int i = ; i < N; i++) //N 不可写为N - 1,否则当N为1时无法进行;
for (int j = i + ; j < N; j++) {
int m = num[i], n = num[j];
if (gcd(m, n) == )
{
count++;
}
}
sum = N * (N - ) / ;
if (count != ) {
double res = sqrt( * sum / count);
printf("%.6f\n",res);
//cout << fixed << setprecision(6) << res << endl;
}
else cout << "No estimate for this data set." << endl;
}
return ;
}

TJU Problem 1100 Pi的更多相关文章

  1. TJU Problem 2101 Bullseye

    注意代码中: result1 << " to " << result2 << ", PLAYER 1 WINS."<& ...

  2. TJU Problem 2548 Celebrity jeopardy

    下次不要被长题目吓到,其实不一定难. 先看输入输出,再揣测题意. 原文: 2548.   Celebrity jeopardy Time Limit: 1.0 Seconds   Memory Lim ...

  3. TJU Problem 2857 Digit Sorting

    原题: 2857.   Digit Sorting Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 3234   Accepted ...

  4. TJU Problem 1015 Gridland

    最重要的是找规律. 下面是引用 http://blog.sina.com.cn/s/blog_4dc813b20100snyv.html 的讲解: 做这题时,千万不要被那个图给吓着了,其实这题就是道简 ...

  5. TJU Problem 1065 Factorial

    注意数据范围,十位数以上就可以考虑long long 了,断点调试也十分重要. 原题: 1065.   Factorial Time Limit: 1.0 Seconds   Memory Limit ...

  6. TJU Problem 2520 Quicksum

    注意: for (int i = 1; i <= aaa.length(); i++) 其中是“ i <= ",注意等号. 原题: 2520.   Quicksum Time L ...

  7. TJU Problem 1090 City hall

    注:对于每一横行的数据读取,一定小心不要用int型,而应该是char型或string型. 原题: 1090.   City hall Time Limit: 1.0 Seconds   Memory ...

  8. TJU Problem 1644 Reverse Text

    注意: int N; cin >> N; cin.ignore(); 同于 int N; scanf("%d\n",&N); 另:关于 cin 与 scanf: ...

  9. Light oj 1100 - Again Array Queries (鸽巢原理+暴力)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1100 给你n个数,数的范围是1~1000,给你q个询问,每个询问问你l到r之间 ...

随机推荐

  1. 使用 XPath 选择器

    在前面的内容中,我们掌握了一些 CSS 选择器和它们的使用方法,以及 rvest 包中用于提取网页内容的函数.一般来说,CSS 选择器足够满足绝大部分的 HTML 节点匹配的需要.但是,当需要根据某些 ...

  2. 《A_Pancers团队》作业5—团队项目需求改进与系统设计

    一:团队项目需求改进 1. <音乐播放软件需求规格说明书>的不足 在上周的<音乐播放软件需求规格说明书>我们还需要改进的地方有:1.搜索功能:2.界面的简洁美观化:3.按键的重 ...

  3. 启动tomcat一闪而过问题调试并解决

    之前一直用myelise+tomcat启动项目很正常,今天在尝试用绿色版tomcat8时,点击startup.bat文件时,一闪而过,tomcat未正常启动,也无法看到错误信息.搜索网络查找到调试方法 ...

  4. unity项目针对IOS及Android平台的音频压缩格式

    IOS : 建议采用MP3格式, Android : 建议采用Vorbis格式, 因为这两种格式分别在这两个平台上有硬件解码的支持, 硬件解码比软件解码快.

  5. Python 爬虫-正则表达式(补)

    2017-08-08 18:37:29 一.Python中正则表达式使用原生字符串的几点说明 原生字符串和普通字符串的不同 相较于普通字符串,原生字符串中的\就是反斜杠,并不表达转义.不过,字符串转成 ...

  6. Python 爬虫-信息的标记xml,json,yaml

    2017-07-26  23:53:03 信息标记的作用有: 标记后的信息可形成信息组织结构,增加了信息维度 标记的结构与信息一样具有重要价值 标记后的信息可用于通信.存储或展示 标记后的信息更利于程 ...

  7. java中的static关键字 学习总结

    使用static关键字修饰的变量和方法为静态变量.静态方法. 非静态方法可以访问静态变量/方法和非静态变量/方法,但静态方法只能访问静态变量/方法. 可以看到在静态方法中调用非静态变量和非静态方法时, ...

  8. Linux系统基本常识

    在虚拟机里装一个Linux(centos),有时间可以装个mac玩一下.(使用centos或者Ubuntu时安装软件将会非常方便) ifconfig –a 显示当前Linux主机的 ip 地址 如何让 ...

  9. FMUtils.KeyboardHook 轻量级键盘监听器

    Nuget添加引用 Install-Package FMUtils.KeyboardHook 调用示例: var KeyboardHook = new Hook("Global Action ...

  10. dp练习(11)——石子并归

    1048 石子归并  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 有n堆石子排成一列,每堆石子有一个重量w ...