Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Precision
- {
- using System;
- using System.Globalization;
- using System.Threading;
- public class Program
- {
- public static void Main()
- {
- Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
- int maxDenominator = int.Parse(Console.ReadLine());
- string number = Console.ReadLine();
- FindFraction(maxDenominator, number);
- }
- public static int Quality(string number, int up, int down)
- {
- int result = 1;
- int current = 2;
- while (true)
- {
- if ((10*up)/down == number[current] - '0')
- {
- result++;
- }
- else
- {
- break;
- }
- current++;
- if (current == number.Length) break;
- up *= 10;
- up %= down;
- }
- return result;
- }
- public static void FindFraction(int maxDenominator, string number)
- {
- string now = "";
- int nowQuality = 0;
- string toDouble = "";
- for (int i = 0; i < 14 && i < number.Length; i++)
- {
- toDouble += number[i];
- }
- double num = Convert.ToDouble(toDouble);
- for (int i = 1; i <= maxDenominator; i++)
- {
- int left = 0, right = i;
- while (right - left > 1)
- {
- int avg = (right + left) / 2;
- double avgVal = (double)avg / i;
- if (avgVal > num) right = avg;
- else left = avg;
- }
- left -= 1;
- right += 1;
- if (left < 0) left = 0;
- if (right > i) right = i;
- for (int k = left; k <= right; k++)
- {
- int current = Quality(number, k, i);
- if (current > nowQuality)
- {
- nowQuality = current;
- now = k.ToString() + '/' + i.ToString();
- }
- }
- }
- Console.WriteLine(now);
- Console.WriteLine(nowQuality);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement