Problem B: Closest Fractions |
Every day we consider lots of real constants: e, π, , to name but a few. These numbers may or may not be rational (i.e. representable as a quotient of two integers), but it is always interesting to give close fraction approximations to them with small denominators and numerators. Arguably the most well-known result in history is from Liu Hui and Zu Chongzhi in China millenniums ago. The result π ≈ 355/113 = 3.141592920 is correct to 7 significant figures, and has remained the most accurate approximation available for the next 900 years.
You are asked to write a program that finds for any input value the three closest fractions x/y, with 1 ≤ x, y ≤ 1000, and x, y relatively prime (i.e. they have no positive common factors other than 1, so the fraction is in simplest form).
Input contains one number on a line, each with exactly ten digits.
For each input number, list the corresponding approximate fractions, ordered by ascending distance to it, as shown in the sample output. You can assume that no ties will occur using our test data.
You are advised to pay attention to the required output format. In particular, all output real numbers must have exactly ten digits.
3.141592654 66.66666666 333.3333333
Input : 3.141592654 1 : 3.141592920 = 355 / 113 2 : 3.141630901 = 732 / 233 3 : 3.141552511 = 688 / 219 Input : 66.66666666 1 : 66.66666667 = 200 / 3 2 : 66.64285714 = 933 / 14 3 : 66.69230769 = 867 / 13 Input : 333.3333333 1 : 333.3333333 = 1000 / 3 2 : 333.5000000 = 667 / 2 3 : 333.0000000 = 333 / 1