Te4nick

Ulam

Dec 21st, 2021
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #pragma warning(disable : 4996)
  5.  
  6. bool isSimple(unsigned short d)
  7. {
  8.     for (int i = 2; i < d / 2 + 1; i++)
  9.     {
  10.         if (d % i == 0) return false;
  11.     }
  12.     return true;
  13. }
  14.  
  15. void Print_Arr(int** Arr, unsigned short n, unsigned short m)
  16. {
  17.     for (int i = 0; i < n; i++)
  18.     {
  19.         for (int j = 0; j < m; j++)
  20.         {
  21.             if (isSimple(Arr[i][j]))
  22.             {
  23.                 printf("%3d", Arr[i][j]);
  24.                 printf("  ");
  25.             }
  26.             else
  27.             {
  28.                 printf("%3c", '*');
  29.                 printf("  ");
  30.             }
  31.            
  32.         }
  33.         printf("\n\n");
  34.     }
  35. }
  36.  
  37. // Заполнить строку или столбец матрицы последовательными числами начиная со start_count.
  38. // a - матрица
  39. // is_row - true - заполнять строку, false - столбец
  40. // col_or_row_num - номер строки или столбца, которые надо заполнить
  41. // begin - начало строки или столбца
  42. // end - конец строки или столбца
  43. // start_count - с какого числа начинаем заполнение
  44. int fill_col_or_row(int** a, short is_row, int col_or_row_num, int begin, int end, int start_count, short n)
  45. {
  46.     if ((start_count > n* n) || (start_count <= 0))
  47.         return start_count;
  48.     int counter = start_count;
  49.     end += (begin < end ? +1 : -1);
  50.     int i;
  51.     for (i = begin; i != end; i += (begin < end ? +1 : -1))
  52.         if (is_row)
  53.             a[col_or_row_num][i] = counter--;
  54.         else
  55.             a[i][col_or_row_num] = counter--;
  56.     return counter;
  57. }
  58.  
  59. // Заполнить матрицу возрастающими натуральными числами по спирали.
  60. // Рекурсивное решение.
  61. // a - матрица
  62. // sphere - номер "витка" спирали в матрице (считаются от нуля), например:
  63. //      0 0 0 0 0
  64. //      0 1 1 1 0
  65. //      0 1 2 1 0
  66. //      0 1 1 1 0
  67. //      0 0 0 0 0
  68. // start_count - с какого числа начинать спираль
  69. void build_spiral(int** a, int sphere, int start_count, unsigned short n)
  70. {
  71.     start_count = fill_col_or_row(a, 0, sphere, sphere, n - 1 - sphere, start_count, n);
  72.     start_count = fill_col_or_row(a, 1, n - 1 - sphere, sphere + 1, n - 1 - sphere, start_count, n);
  73.     start_count = fill_col_or_row(a, 0, n - 1 - sphere, n - 2 - sphere, sphere, start_count, n);
  74.     start_count = fill_col_or_row(a, 1, sphere, n - 2 - sphere, sphere + 1, start_count, n);
  75.     if (sphere < n / 2) build_spiral(a, sphere + 1, start_count, n);
  76.     if (start_count == 0)
  77.         return;
  78. }
  79.  
  80. int main()
  81. {
  82.     setlocale(0, "");
  83.     register unsigned short i;
  84.     int n = 0;
  85.     printf("Введите размер матрицы (установиться размер 2*n+1): ");
  86.     scanf("%d", &n);
  87.     n = 2 * n + 1;
  88.     int** Arr = (int**)malloc(sizeof(int*) * n);
  89.     for (i = 0; i < n; i++)
  90.         Arr[i] = (int*)malloc(sizeof(int) * n);
  91.     build_spiral(Arr, 0, n * n, n);
  92.     Print_Arr(Arr, n, n);
  93.  
  94.     free(Arr);
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment