Advertisement
fferum

Untitled

Apr 30th, 2020
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.04 KB | None | 0 0
  1. /*
  2. Author: Филипповых Матвей
  3. Group: СБС-901
  4. вариант: 4.2
  5. Description: Каждая строка в двумерной матрице представляет собой двоичное число(элементы строки могут принимать только два значения - нуль или единица).
  6. Найти номера строк, модуль разности чисел которых - максимален.
  7. */
  8. #include <cstdio>
  9. #include <clocale>
  10. #include <cstdlib>
  11. #include <ctime>
  12. #include <locale>
  13. #include<iomanip>
  14. /*void outputting(int lines, int column, int the_binary_number_system, double A[][])
  15. {
  16.     for (int i = 0; i < lines; i++)
  17.     {
  18.         printf("\nA[%d]=", i + 1);
  19.         for (int j = 0; j < column; j++)
  20.         {
  21.             the_binary_number_system = A[i + 1][j + 1];
  22.             printf("%d ", the_binary_number_system);
  23.         }
  24.     }
  25. }*/
  26.  
  27.  
  28. void Outputting_Matrix(double* Element_of_array[], const int stroki, const int stolb) {
  29.     for (int j = 0; j < stolb; j++) {
  30.         if (j == 0) {
  31.             printf(" %d\t", j + 1);
  32.         }
  33.         else
  34.             printf("%d\t", j + 1);
  35.     }
  36.     for (int i = 0; i < stroki; i++) {
  37.         printf("\n");
  38.         printf("%d|", i + 1);
  39.         for (int j = 0; j < stolb; j++) {
  40.             printf("%lf", Element_of_array[i][j]);
  41.         }
  42.     }
  43.     printf("\n");
  44.  
  45. }
  46.  
  47. int random()
  48. {
  49.     double rnd = double(rand()) / RAND_MAX;
  50.     int rnd2;
  51.     if (rnd > 0.5) {
  52.         rnd2 = 1;
  53.     }
  54.     else {
  55.         rnd2 = 0;
  56.     }
  57.     printf("%d", rnd2);
  58.     return rnd2;
  59. }
  60. int main()
  61. {
  62.     setlocale(LC_ALL, "Russian");
  63.     const size_t MAX_SIZE = 101;
  64.     double A[MAX_SIZE][MAX_SIZE];
  65.     srand(time(NULL));
  66.     /*спрашиваем сколько строк и стобцов*/
  67.     printf("Введите количесвто строк \n");
  68.     int lines;
  69.     scanf_s("%d", &lines);
  70.     while (lines < 0 || lines>MAX_SIZE)
  71.     {
  72.         printf("нельзя вводить отрицательные числа и числа больше 1000 строк\n");
  73.         printf("Введите количествово строк");
  74.         scanf_s("%d", &lines);
  75.     }
  76.  
  77.     printf("Введите количесвто столбцов \n");
  78.     int column;
  79.     scanf_s("%d", &column);
  80.     while (column < 0 || column>MAX_SIZE)
  81.     {
  82.         printf("нельзя вводить отрицательные числа и числа больше 1000 для столбцов\n");
  83.         printf("Введите количествово столбцов");
  84.         scanf_s("%d", &column);
  85.     }
  86.  
  87.  
  88.  
  89.  
  90.     /*вводим числа в матрицу*/
  91.     printf("хотите сами ввести числа в матрицу? если да напишите\"1\" иначе любую другую цифру");
  92.     int answer = 0;
  93.     scanf_s("%d", &answer);
  94.     int the_binary_number_system;
  95.     if (answer == 1)
  96.     {
  97.         for (int i = 0; i < lines; i++)
  98.         {
  99.             for (int j = 0; j < column; j++)
  100.             {
  101.                 printf("\nA[%d][%d]=", i + 1, j + 1);
  102.                 scanf_s("%d", &the_binary_number_system);
  103.                 while (the_binary_number_system != 0 && the_binary_number_system != 1)
  104.                 {
  105.                     printf("нельзя вводить не двоичные числа \n");
  106.                     scanf_s("%d", &the_binary_number_system);
  107.                 }
  108.                 A[i + 1][j + 1] = the_binary_number_system;
  109.             }
  110.         }
  111.         /*вывод на консоль*/
  112.         double* matr[MAX_SIZE];
  113.         for (int i = 0; i < lines; i++)
  114.             matr[i] = A[i];
  115.         Outputting_Matrix(matr, lines, column);
  116. /*      for (int i = 0; i < lines; i++)
  117.         {
  118.             printf("\nA[%d]=", i + 1);
  119.             for (int j = 0; j < column; j++)
  120.             {
  121.                 the_binary_number_system = A[i + 1][j + 1];
  122.                 printf("%d ", the_binary_number_system);
  123.             }
  124.         }
  125.         */
  126.     }
  127.     else
  128.     {
  129.         for (int i = 0; i < lines; i++)
  130.         {
  131.             for (int j = 0; j < column; j++)
  132.             {
  133.                 A[i + 1][j + 1] = random();
  134.             }
  135.         }
  136.         /*вывод на консоль*/
  137.         for (int i = 0; i < lines; i++)
  138.         {
  139.             printf("\nA[%d]=", i + 1);
  140.             for (int j = 0; j < column; j++)
  141.             {
  142.                 the_binary_number_system = A[i + 1][j + 1];
  143.                 printf("%d ", the_binary_number_system);
  144.             }
  145.         }
  146.     }
  147.     /*начинается алгоритм счета*/
  148.     int B[MAX_SIZE];
  149.     /*перевод из двоичного в десятиричное*/
  150.     for (int i = 0; i < lines; i++)
  151.     {
  152.         int the_number_of_the_decimal_system = 0;
  153.         for (int j = 0; j < column; j++)
  154.         {
  155.             the_binary_number_system = A[i + 1][column - j];
  156.             the_number_of_the_decimal_system = the_binary_number_system * pow(2, j) + the_number_of_the_decimal_system;
  157.  
  158.  
  159.         }
  160.         B[i + 1] = the_number_of_the_decimal_system;
  161.     }
  162.     /*нахождение максимального модуля разности*/
  163.     int max = 0;
  164.     for (int i = 0; i < lines-1; i++)
  165.     {
  166.         for (int j=1 ; j < lines; j++) {
  167.             int first_number = B[i + 1];
  168.             int second_number = B[j+1];
  169.             if (max < abs(first_number - second_number)) {
  170.                 max = abs(first_number - second_number);
  171.             }
  172.         }
  173.     }
  174.  
  175.  
  176.     /*выписывание ответа*/
  177.     for (int i = 0; i < lines-1; i++)
  178.     {
  179.         for (int j=1; j < lines; j++) {
  180.             int first_number = B[i + 1];
  181.             int second_number = B[j+1];
  182.             if (max <= abs(first_number - second_number)) {
  183.                 printf("\nA[%d] и A[%d]", i + 1, j+1);
  184.             }
  185.         }
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement