Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //левый верхний по часовой одномерный
- //#include "pch.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int const strs = 7;
- int const clmns = 3;
- void init_m(int A[], int strs, int clmns)
- {
- for (int i = 0; i < strs; i++)
- {
- for (int j = 0; j < clmns; j++)
- {
- A[i*clmns + j] = rand() % 20;
- }
- }
- }
- void print_m(int A[], int strs, int clmns)
- {
- for (int i = 0; i < strs; i++)
- {
- for (int j = 0; j < clmns; j++)
- {
- printf("%4d", A[i*clmns + j]);
- }
- printf("\n");
- }
- printf("\n");
- }
- void left_down(int A[][clmns],int strs)
- {
- int i, j;
- int limit = strs * clmns;
- int left = 0;
- int right = clmns - 1;
- int up = 0;
- int down = strs - 1;
- int Strs = strs;
- int Clmns = clmns;
- while ((limit > 0) && (left <= right) && (up <= down))
- {
- for (i = up; i < down; i++)
- {
- printf("%d ", A[i][left]);
- limit--;
- if (limit == 0)
- {
- break;
- }
- }
- for (j = left; j < right; j++)
- {
- printf("%d ", A[down][j]);
- limit--;
- if (limit == 0)
- {
- break;
- }
- }
- for (i = down; i > up; i--)
- {
- printf("%d ", A[i][right]);
- limit--;
- if (limit == 0)
- {
- break;
- }
- }
- for (j = right; j > left; j--)
- {
- printf("%d ", A[up][j]);
- limit--;
- if (limit == 0)
- {
- break;
- }
- }
- left++;
- right--;
- up++;
- down--;
- }
- if(limit == 1)
- printf("%d ", A[Strs/2][Clmns/2]);
- }
- int main()
- {
- srand(12345);
- int* A = (int*)malloc(clmns * strs * sizeof(int));
- init_m(A, strs, clmns);
- print_m(A, strs, clmns);
- left_down(A,strs);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement