Advertisement
svetlai

PrecisionAuthor

Nov 13th, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1. namespace Precision
  2. {
  3.     using System;
  4.     using System.Globalization;
  5.     using System.Threading;
  6.  
  7.     public class Program
  8.     {
  9.         public static void Main()
  10.         {
  11.             Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
  12.             int maxDenominator = int.Parse(Console.ReadLine());
  13.             string number = Console.ReadLine();
  14.             FindFraction(maxDenominator, number);
  15.         }
  16.  
  17.         public static int Quality(string number, int up, int down)
  18.         {
  19.             int result = 1;
  20.  
  21.             int current = 2;
  22.             while (true)
  23.             {
  24.                 if ((10*up)/down == number[current] - '0')
  25.                 {
  26.                     result++;
  27.                 }
  28.                 else
  29.                 {
  30.                     break;
  31.                 }
  32.  
  33.                 current++;
  34.                 if (current == number.Length) break;
  35.                 up *= 10;
  36.                 up %= down;
  37.             }
  38.  
  39.             return result;
  40.         }
  41.  
  42.         public static void FindFraction(int maxDenominator, string number)
  43.         {
  44.             string now = "";
  45.             int nowQuality = 0;
  46.             string toDouble = "";
  47.  
  48.             for (int i = 0; i < 14 && i < number.Length; i++)
  49.             {
  50.                 toDouble += number[i];
  51.             }
  52.  
  53.             double num = Convert.ToDouble(toDouble);
  54.  
  55.             for (int i = 1; i <= maxDenominator; i++)
  56.             {
  57.                 int left = 0, right = i;
  58.                 while (right - left > 1)
  59.                 {
  60.                     int avg = (right + left) / 2;
  61.                     double avgVal = (double)avg / i;
  62.                     if (avgVal > num) right = avg;
  63.                     else left = avg;
  64.                 }
  65.  
  66.                 left -= 1;
  67.                 right += 1;
  68.                 if (left < 0) left = 0;
  69.                 if (right > i) right = i;
  70.  
  71.                 for (int k = left; k <= right; k++)
  72.                 {
  73.                     int current = Quality(number, k, i);
  74.                     if (current > nowQuality)
  75.                     {
  76.                         nowQuality = current;
  77.                         now = k.ToString() + '/' + i.ToString();
  78.                     }
  79.                 }
  80.             }
  81.  
  82.             Console.WriteLine(now);
  83.             Console.WriteLine(nowQuality);
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement