Advertisement
dllbridge

Untitled

Dec 2nd, 2022
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.17 KB | None | 0 0
  1.  
  2.  
  3. #include   <stdio.h>
  4.  
  5.  
  6.  
  7. float  fRes;  //  Будет максимально приближаться (слева) к 3/7
  8.  
  9. int   k_Res,
  10.       p_Res;
  11.  
  12.  
  13.  
  14. int test(int); //  Тестирует функцию "checking_the_correct_fraction()"
  15.                //   несколькими предопределёнными дробями.
  16.                //  Если дроби все правильные, то возвращает  1  (один)
  17.                //  Если хотя бы одна неправильная, то вернёт 0  (ноль)
  18.  
  19.  
  20. //  Возвращает:
  21. //  = 0, если дробь неправильная
  22. //  = 1, если правильная
  23. int checking_the_correct_fraction(int k, int p);
  24.  
  25.  
  26.  
  27. //  Ищет все правильные дроби со знаменателем >= n
  28. void Enumeration(int n);
  29.  
  30. ////////////////////////////////////////////////////
  31. int main()                                        //
  32. {
  33.  
  34.   //  if(test(2) == 1) printf("Test. All fractions are correct. \n");             // Удалить после тестирования ф-ции.
  35.   //  else             printf("Test. Not all fractions are correct !!! \n");      // Удалить после тестирования ф-ции.
  36.  
  37.     printf("We print all the correct fractions up to the denominator equal to 8.\n");
  38.     printf("In ascending order of the denominator.\n");
  39.    
  40.     Enumeration(8);
  41.    
  42.     printf("In the list above, the fraction closest to 3/7 on the left is:\n%d/%d = %.3f \n",
  43.                                                                         k_Res, p_Res, fRes);  
  44.  
  45.  
  46.     Enumeration(800);
  47.    
  48.     printf("If the denominator reaches 800, the fraction closest to 3/7 on the left is:\n%d/%d = %.3f \n",
  49.                                                                         k_Res, p_Res, fRes);  
  50.                                                                        
  51.     Enumeration(10000);
  52.    
  53.     printf("If the denominator reaches 10 000, the fraction closest to 3/7 on the left is:\n%d/%d = %.3f \n",
  54.                                                                         k_Res, p_Res, fRes);                                                                        
  55. }
  56.  
  57.  
  58.  
  59. float      _f;  //      частное (вещественное число)         
  60.                
  61. ////////////////////////////////////////////////////
  62. void the_closest_one_on_the_left(int k, int p)    //
  63. {
  64.    
  65.      if(_f > fRes )
  66.      if(_f < 0.428)
  67.      {
  68.         fRes  = _f;
  69.         k_Res =  k;
  70.         p_Res =  p;
  71.      }
  72. }
  73.  
  74.  
  75.  
  76. //  Ищет все правильные дроби со знаменателем >= n
  77. ////////////////////////////////////////////////////
  78. void Enumeration(int n)                           //
  79. {
  80.    
  81.      float         fk,  //    числитель (вещественное число)
  82.                    fp;  //  знаменатель (вещественное число)
  83.    
  84.      fRes = 0;                                    // Будет максимально приближаться (слева) к 3/7
  85.    
  86.      for(int p = 2; p <= n; p++)                  // Знаменатели переберём по возрастанию
  87.      for(int k = 2; k <  n; k++)                  // Перебор числителей
  88.      {
  89.            
  90.         if(checking_the_correct_fraction(k, p) == 1)  // Если очередная дробь правильная...
  91.         {  
  92.            
  93.            fk =     k; 
  94.            fp =     p;  
  95.           _f  = fk/fp;
  96.          
  97.            the_closest_one_on_the_left(k, p);
  98.            
  99.            if(n < 10) printf("%d/%d = %.3f\n", k, p, _f); // Печатаем список всех дробей, если знаменатель < 10
  100.         }  
  101.      } 
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148. //  Возвращает:
  149. //  = 0, если дробь неправильная
  150. //  = 1, если правильная 
  151. ////////////////////////////////////////////////////  k - это числитель
  152. int checking_the_correct_fraction(int k, int p)   //  p -   знаменатель
  153. {
  154.    
  155.     if(k >= p)  return 0;                         //  Числитель не меньше знаменателя
  156.    
  157.     while (p > 0)
  158.     {
  159.         int c = k % p;                            //  Алгоритм Евклида (НОД)
  160.         k = p;
  161.         p = c;
  162.     }
  163.    
  164.     if(k != 1) return 0;                          //  Есть общий множитель больше 1 (единицы)
  165.    
  166. return 1;    
  167. }
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176. //  Тестирует функцию "checking_the_correct_fraction()"
  177. //  несколькими предопределёнными дробями.
  178. //  Если дроби все правильные, то возвращает  1  (один)
  179. //  Если хотя бы одна неправильная, то вернёт 0  (ноль)
  180. //////////////////////////////////////////////////////////////////////
  181. int test(int n)                                                     //
  182. {
  183.    
  184.     switch(n)
  185.     {
  186.    
  187.        case  1:  //  Есть неправильные дроби
  188.    
  189.         if(checking_the_correct_fraction( 2,  5) == 0) return 0;          
  190.         if(checking_the_correct_fraction( 3,  7) == 0) return 0;
  191.         if(checking_the_correct_fraction(13, 14) == 0) return 0;
  192.         if(checking_the_correct_fraction(19, 20) == 0) return 0;    
  193.         if(checking_the_correct_fraction( 2,  6) == 0) return 0;
  194.         if(checking_the_correct_fraction( 3,  9) == 0) return 0;
  195.         if(checking_the_correct_fraction(13, 26) == 0) return 0;
  196.         if(checking_the_correct_fraction( 5, 20) == 0) return 0;    
  197.         return 1;
  198.        
  199.        case  2:  //  Все дроби правильные
  200.    
  201.         if(checking_the_correct_fraction( 2,  5) == 0) return 0;        
  202.         if(checking_the_correct_fraction( 3,  7) == 0) return 0;
  203.         if(checking_the_correct_fraction(13, 14) == 0) return 0;
  204.         if(checking_the_correct_fraction(19, 20) == 0) return 0;    
  205.         if(checking_the_correct_fraction( 5,  7) == 0) return 0;
  206.         if(checking_the_correct_fraction(13, 14) == 0) return 0;
  207.         if(checking_the_correct_fraction(15, 26) == 0) return 0;
  208.         if(checking_the_correct_fraction( 5, 19) == 0) return 0;    
  209.         return 1;  
  210.     }      
  211. }
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement