TJU Problem 1100 Pi

注:

  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

源代码:

 1 #include <iostream>
 2 #include <iomanip>
 3 #include <cmath>
 4 #include <stdio.h>
 5 using namespace std;
 6
 7 int num[55];
 8
 9 int gcd(int a, int b)    {
10     return b == 0 ? a : gcd(b, a % b);
11 }
12
13 int main()
14 {
15     int N;
16     while (cin >> N && N != 0)    {
17         int count = 0; double sum = 0;
18         for (int i = 0; i < N; i++)    cin >> num[i];
19         for (int i = 0; i < N; i++)         //N 不可写为N - 1,否则当N为1时无法进行;
20             for (int j = i + 1; j < N; j++)    {
21                 int m = num[i], n = num[j];
22                 if (gcd(m, n) == 1)
23                 {
24                     count++;
25                 }
26             }
27         sum = N * (N - 1) / 2;
28         if (count != 0)    {
29             double res = sqrt(6 * sum / count);
30             printf("%.6f\n",res);
31             //cout << fixed << setprecision(6) << res << endl;
32         }
33         else cout << "No estimate for this data set." << endl;
34     }
35     return 0;
36 }
时间: 2024-10-31 00:30:14

TJU Problem 1100 Pi的相关文章

TJU Problem 1065 Factorial

注意数据范围,十位数以上就可以考虑long long 了,断点调试也十分重要. 原题: 1065.   Factorial Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 6067   Accepted Runs: 2679 The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceiver

TJU Problem 2520 Quicksum

注意: for (int i = 1; i <= aaa.length(); i++) 其中是“ i <= ",注意等号. 原题: 2520.   Quicksum Time Limit: 0.5 Seconds   Memory Limit: 65536KTotal Runs: 2964   Accepted Runs: 1970 A checksum is an algorithm that scans a packet of data and returns a single

TJU Problem 1015 Gridland

最重要的是找规律. 下面是引用 http://blog.sina.com.cn/s/blog_4dc813b20100snyv.html 的讲解: 1 做这题时,千万不要被那个图给吓着了,其实这题就是道简单的数学题. 2 3 首先看当m或n中有一个为2的情况,显然,只需要算周长就OK了.即(m+n-2)*2,考虑到至少其中一个为2,所以答案为2 *m或2*n,亦即m*n.注意这里保证了其中一个数位偶数. 4 当m,n≥3时,考虑至少其中一个为偶数的情况,显然,这种情况很简单,可以得出,结果为m*

TJU Problem 2857 Digit Sorting

原题: 2857.   Digit Sorting Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 3234   Accepted Runs: 1704 Several players play a game. Each player chooses a certain number, writes it down (in decimal notation, without leading zeroes) and sorts t

TJU Problem 2101 Bullseye

注意代码中: result1 << " to " << result2 << ", PLAYER 1 WINS."<< endl; 和 result1 << " to " << result2 << ", PLAYER 1 WINS. "<< endl; 虽然只在WINS后只差一个空格,但会导致PE. 原题: 2101.   Bul

TJU Problem 2548 Celebrity jeopardy

下次不要被长题目吓到,其实不一定难. 先看输入输出,再揣测题意. 原文: 2548.   Celebrity jeopardy Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 1306   Accepted Runs: 898 It's hard to construct a problem that's so easy that everyone will get it, yet still difficult enough

【水】TJU Problem 1805 Electrical Outlets

没看懂题,直接看了INPUT,原来真的可以猜出来. 原题: 1805.   Electrical Outlets Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 3321   Accepted Runs: 2575 Roy has just moved into a new apartment. Well, actually the apartment itself is not very new, even dating ba

TJU Problem 1644 Reverse Text

注意: int N; cin >> N; cin.ignore(); 同于 int N; scanf("%d\n",&N); 另:关于 cin 与 scanf: scanf是格式化输入,printf是格式化输出. cin是输入流,cout是输出流.效率稍低,但书写简便. 格式化输出效率比较高,但是写代码麻烦. 流输出操作效率稍低,但书写简便. cout之所以效率低,正如一楼所说,是先把要输出的东西存入缓冲区,再输出,导致效率降低. 缓冲区比较抽象,举个例子吧: 曾经

TJU Problem 1090 City hall

注:对于每一横行的数据读取,一定小心不要用int型,而应该是char型或string型. 原题: 1090.   City hall Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 4874   Accepted Runs: 2395 Because of its age, the City Hall has suffered damage to one of its walls. A matrix with M rows an