Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. class Solution
  6. {
  7.     public const double PI = 3.1415926535897932384626433832795028841971693993751;
  8.  
  9.     static void Main(String[] args)
  10.     {
  11.         string[] tokens_min = Console.ReadLine().Split(' ');
  12.         long min = Convert.ToInt64(tokens_min[0]);
  13.         long max = Convert.ToInt64(tokens_min[1]);
  14.         //--- my code ---
  15.         double n = 1;
  16.         double d = 1;
  17.  
  18.         for (double i = min; i < max; i++)
  19.         {
  20.             double difference = 1;
  21.             double minimal = 10;
  22.             for (double j = 1; (j / d) <= PI; j++)
  23.             {
  24.                 Console.WriteLine("{0}/{1}", j, i);
  25.                 double lowerBound = j - 1;
  26.                 double upperBound = j;
  27.                 double lowerDifference = Math.Abs(lowerBound / d - PI);
  28.                 double upperDifference = Math.Abs(lowerBound / d - PI);
  29.                 /*Console.WriteLine("For lower boundary {2}, the difference is {0}" +
  30.                     "\n For upper boundary {3}, the difference is {1}",
  31.                     lowerDifference, upperDifference, lowerBound, upperBound);
  32.                 */
  33.                 if (lowerDifference < upperDifference)
  34.                 {
  35.                     difference = lowerDifference;
  36.                     if (difference < minimal)
  37.                     {
  38.                         minimal = difference;
  39.                         d = i;
  40.                         n = lowerBound;
  41.                         Console.WriteLine("Minimal value: {0}", minimal);
  42.                     }
  43.                 }
  44.                 else
  45.                 {
  46.                     difference = upperDifference;
  47.                     if (difference < minimal)
  48.                     {
  49.                         minimal = difference;
  50.                         d = i;
  51.                         n = upperBound;
  52.                         Console.WriteLine("Minimal value: {0}", minimal);
  53.                     }
  54.                 }
  55.             }
  56.         }
  57.         Console.WriteLine("{0}/{1}", n, d);
  58.         Console.ReadLine();
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement