Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- #pragma warning(disable : 4996)
- bool isSimple(unsigned short d)
- {
- for (int i = 2; i < d / 2 + 1; i++)
- {
- if (d % i == 0) return false;
- }
- return true;
- }
- void Print_Arr(int** Arr, unsigned short n, unsigned short m)
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- if (isSimple(Arr[i][j]))
- {
- printf("%3d", Arr[i][j]);
- printf(" ");
- }
- else
- {
- printf("%3c", '*');
- printf(" ");
- }
- }
- printf("\n\n");
- }
- }
- // Заполнить строку или столбец матрицы последовательными числами начиная со start_count.
- // a - матрица
- // is_row - true - заполнять строку, false - столбец
- // col_or_row_num - номер строки или столбца, которые надо заполнить
- // begin - начало строки или столбца
- // end - конец строки или столбца
- // start_count - с какого числа начинаем заполнение
- int fill_col_or_row(int** a, short is_row, int col_or_row_num, int begin, int end, int start_count, short n)
- {
- if ((start_count > n* n) || (start_count <= 0))
- return start_count;
- int counter = start_count;
- end += (begin < end ? +1 : -1);
- int i;
- for (i = begin; i != end; i += (begin < end ? +1 : -1))
- if (is_row)
- a[col_or_row_num][i] = counter--;
- else
- a[i][col_or_row_num] = counter--;
- return counter;
- }
- // Заполнить матрицу возрастающими натуральными числами по спирали.
- // Рекурсивное решение.
- // a - матрица
- // sphere - номер "витка" спирали в матрице (считаются от нуля), например:
- // 0 0 0 0 0
- // 0 1 1 1 0
- // 0 1 2 1 0
- // 0 1 1 1 0
- // 0 0 0 0 0
- // start_count - с какого числа начинать спираль
- void build_spiral(int** a, int sphere, int start_count, unsigned short n)
- {
- start_count = fill_col_or_row(a, 0, sphere, sphere, n - 1 - sphere, start_count, n);
- start_count = fill_col_or_row(a, 1, n - 1 - sphere, sphere + 1, n - 1 - sphere, start_count, n);
- start_count = fill_col_or_row(a, 0, n - 1 - sphere, n - 2 - sphere, sphere, start_count, n);
- start_count = fill_col_or_row(a, 1, sphere, n - 2 - sphere, sphere + 1, start_count, n);
- if (sphere < n / 2) build_spiral(a, sphere + 1, start_count, n);
- if (start_count == 0)
- return;
- }
- int main()
- {
- setlocale(0, "");
- register unsigned short i;
- int n = 0;
- printf("Введите размер матрицы (установиться размер 2*n+1): ");
- scanf("%d", &n);
- n = 2 * n + 1;
- int** Arr = (int**)malloc(sizeof(int*) * n);
- for (i = 0; i < n; i++)
- Arr[i] = (int*)malloc(sizeof(int) * n);
- build_spiral(Arr, 0, n * n, n);
- Print_Arr(Arr, n, n);
- free(Arr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment