Advertisement
Dance-Decadence

w1

Feb 27th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h> // в ней функция time
  3. #include <stdlib.h>
  4. #define MAX 64
  5.  
  6. using namespace std;
  7.  
  8. void print_line(int count)  {   // Разделительная линия
  9.     cout << "--";
  10.     int p = 1;
  11.     while (p < count)  {
  12.         p++;
  13.         cout << "---";
  14.     }
  15.     cout << endl;
  16. }
  17.  
  18. void enter_matrix_random(int (&mas)[MAX][MAX], int &N)  {   // Ввод матрицы рандомом
  19.     srand(time(NULL)); // Рандом рандома
  20.     cout << "Введите разрядность матрицы = ";
  21.     cin >> N;  
  22.     print_line(N);
  23.     for (int i = 0; i < N; i++)  {
  24.         for (int j = 0; j < N; j++)  {
  25.             mas[i][j] = 10 + rand() % 90; // Каждый элемент случайному числу от 10 до 99
  26.         }
  27.     }
  28. }
  29.  
  30. void enter_matrix_hand(int (&mas)[MAX][MAX], int &N)  {   // Ввод матрицы от руки
  31.     cout << "Введите разрядность матрицы = ";
  32.     cin >> N;  
  33.     print_line(N);
  34.     for (int i = 0; i < N; i++)  {
  35.         for (int j = 0; j < N; j++)  {
  36.             cout << "Введите элемент массива (двузначное число) [" << i << "][" << j << "] = ";
  37.             cin >> mas[i][j];
  38.             cout << endl;
  39.         }
  40.     }
  41. }
  42.  
  43. void choice(int (&mas)[MAX][MAX], int &N)  {   // Выбор способа ввода матрицы
  44.     cout << "Заполнить матрицу случайными числами? [y/n] : ";
  45.     string y = "y";
  46.     string n = "n";
  47.     string c;
  48.     cin >> c;
  49.     if (c == y)  {
  50.         cout << "Заполнение случайными числами: " << endl;  
  51.         enter_matrix_random(mas, N);
  52.     }
  53.     else if (c == n)  {
  54.         cout << "Заполнение от руки:" << endl;
  55.         enter_matrix_hand(mas, N);
  56.     }
  57.     else
  58.     cout << "Ошибка!!!" << endl;
  59. }
  60.  
  61. void print_matrix (int mas[MAX][MAX], int N, string message)  {   // Печать матрицы
  62.     cout << message << endl;
  63.     print_line(N);
  64.     for (int i = 0; i < N; i++)  {
  65.         for (int j = 0; j < N; j++)  {
  66.             cout << mas[i][j] << " ";
  67.         }
  68.         cout << endl;
  69.     }
  70.     print_line(N);
  71. }
  72.  
  73. void quicksort (int a[MAX], int left, int right)  {   // Быстрая сортировка
  74.     int l = left, r = right;
  75.     int swap;
  76.     int x = a[(left+right) / 2];
  77.     do  {
  78.         while (a[l] < x)    l++;
  79.         while (a[r] > x)    r--;
  80.         if (l <= r)  {
  81.             if (a[l] > a[r])  {
  82.                 swap = a[l];
  83.                 a[l] = a[r];
  84.                 a[r] = swap;
  85.             }  
  86.             l++; r--;
  87.         }
  88.     }
  89.     while (l <= r);
  90.     if (l < right) quicksort(a, l, right);
  91.     if (left < r) quicksort(a, left, r);
  92. }
  93.  
  94. void changing_matrix(int (&a)[MAX], int mas[MAX][MAX], int &count, int N)  {   // Чтение элементов массива   
  95.     for (int j=0; j<N; j++)     {
  96.         int i=0;
  97.         a[count] = mas[i][j];
  98.         count++;
  99.     }
  100.     for (int i=1; i<N-1; i++)  {
  101.         int j=N-1;
  102.         a[count] = mas[i][j];
  103.         count++;
  104.     }
  105.     for (int j=N-1; j>0; j--)  {
  106.         int i=N-1;
  107.         a[count] = mas[i][j];
  108.         count++;
  109.     }
  110.     for (int i=N-1; i>0; i--)  {
  111.         int j=0;
  112.         a[count] = mas[i][j];
  113.         count++;
  114.     }
  115.     /*cout << "Элементы сортировки: ";
  116.     for (int i = 0; i < count; i++)  {
  117.         cout << a[i] << " ";
  118.     }
  119.     cout << endl;*/
  120.     quicksort(a, 0, count-1);
  121.     /*cout << "Элементы после сортировки: ";
  122.     for (int i = 0; i < count; i++)  {
  123.         cout << a[i] << " ";
  124.     }
  125.     cout << endl;*/
  126.     count = 0;
  127.     for (int j=0; j<N; j++)  {
  128.         int i=0;
  129.         mas[i][j] = a[count];
  130.         count++;
  131.     }
  132.     for (int i=1; i<N-1; i++)  {
  133.         int j=N-1;
  134.         mas[i][j] = a[count];
  135.         count++;
  136.     }
  137.     for (int j=N-1; j>0; j--)  {
  138.         int i=N-1;
  139.         mas[i][j] = a[count];
  140.         count++;
  141.     }
  142.     for (int i=N-1; i>0; i--)  {
  143.         int j=0;
  144.         mas[i][j] = a[count];
  145.         count++;
  146.     }
  147. }
  148.  
  149. void finding_doubles(int mas[MAX][MAX], int N)  {
  150.     for (int j=0; j<N; j++)  { 
  151.         for (int i=0; i<N; i++)  {
  152.             for (int k=i+1; k<N; k++)  {
  153.                 if (mas[i][j] == mas [k][j])
  154.                 cout << "Номер столбца с повторениями: " << j+1 << " (повторение = " << mas [i][j] << ")" << endl;
  155.             }
  156.         }
  157.     }  
  158. }
  159.  
  160. int main()  {
  161.     int matrix[MAX][MAX]; // Матрица
  162.     int n; // Разрядность матрицы
  163.     choice(matrix, n);
  164.     print_matrix(matrix, n, "Исходная матрица: ");
  165.     int reqmas[MAX];  // Массив элементов сортировки
  166.     int reqcount = 0; // Количество элементов сортировки
  167.     changing_matrix(reqmas, matrix, reqcount, n);
  168.     print_matrix(matrix, n, "Матрица после сортировки: ");
  169.     finding_doubles(matrix, n);
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement