Загрузка данных


#include <stdio.h>
#include <stdlib.h>

int input(int **a, int *n);
void output(int *a, int n);
void sort(int *a, int n);
void swap(int *a, int *b);

int main(void) {
    int n;
    int *data = NULL;

    if (input(&data, &n)) {
        sort(data, n);
        output(data, n);
        free(data);
    } else {
        printf("n/a");
        free(data);
    }

    return 0;
}

int input(int **a, int *n) {
    int result = 1;

    if (scanf("%d", n) != 1 || *n <= 0) {
        result = 0;
    }

    if (result) {
        *a = malloc(*n * sizeof(int));
        if (*a == NULL) {
            result = 0;
        }
    }

    for (int i = 0; result && i < *n; i++) {
        if (scanf("%d", *a + i) != 1) {
            result = 0;
        }
    }

    return result;
}

void output(int *a, int n) {
    for (int i = 0; i < n; i++) {
        if (i > 0) {
            printf(" ");
        }
        printf("%d", *(a + i));
    }
}

void sort(int *a, int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (*(a + j) > *(a + j + 1)) {
                swap(a + j, a + j + 1);
            }
        }
    }
}

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}


#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100

int input_size(int *rows, int *cols);
int input_matrix(int **matrix, int rows, int cols);
void output_matrix(int **matrix, int rows, int cols);
int **allocate_dynamic_1(int rows, int cols);
int **allocate_dynamic_2(int rows, int cols);
int **allocate_dynamic_3(int rows, int cols);
void free_matrix(int **matrix, int type, int rows);

int main(void) {
    int type, rows, cols;
    int static_matrix[MAX_SIZE][MAX_SIZE];
    int *static_ptrs[MAX_SIZE];
    int **matrix = NULL;

    if (scanf("%d", &type) != 1 || type < 1 || type > 4 || !input_size(&rows, &cols)) {
        printf("n/a");
        return 1;
    }

    if (type == 1) {
        if (rows > MAX_SIZE || cols > MAX_SIZE) {
            printf("n/a");
            return 1;
        }
        for (int i = 0; i < rows; i++) {
            static_ptrs[i] = static_matrix[i];
        }
        matrix = static_ptrs;
    } else if (type == 2) {
        matrix = allocate_dynamic_1(rows, cols);
    } else if (type == 3) {
        matrix = allocate_dynamic_2(rows, cols);
    } else {
        matrix = allocate_dynamic_3(rows, cols);
    }

    if (matrix == NULL || !input_matrix(matrix, rows, cols)) {
        printf("n/a");
        free_matrix(matrix, type, rows);
        return 1;
    }

    output_matrix(matrix, rows, cols);
    free_matrix(matrix, type, rows);

    return 0;
}

int input_size(int *rows, int *cols) {
    return scanf("%d %d", rows, cols) == 2 && *rows > 0 && *cols > 0;
}

int input_matrix(int **matrix, int rows, int cols) {
    int result = 1;

    for (int i = 0; result && i < rows; i++) {
        for (int j = 0; result && j < cols; j++) {
            if (scanf("%d", *(matrix + i) + j) != 1) {
                result = 0;
            }
        }
    }

    return result;
}

void output_matrix(int **matrix, int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        if (i > 0) {
            printf("\n");
        }
        for (int j = 0; j < cols; j++) {
            if (j > 0) {
                printf(" ");
            }
            printf("%d", *(*(matrix + i) + j));
        }
    }
}

int **allocate_dynamic_1(int rows, int cols) {
    int **matrix = malloc(rows * sizeof(int *));

    if (matrix != NULL) {
        for (int i = 0; i < rows; i++) {
            *(matrix + i) = malloc(cols * sizeof(int));
            if (*(matrix + i) == NULL) {
                for (int j = 0; j < i; j++) {
                    free(*(matrix + j));
                }
                free(matrix);
                matrix = NULL;
                break;
            }
        }
    }

    return matrix;
}

int **allocate_dynamic_2(int rows, int cols) {
    int **matrix = malloc(rows * sizeof(int *));
    int *data = NULL;

    if (matrix != NULL) {
        data = malloc(rows * cols * sizeof(int));
        if (data == NULL) {
            free(matrix);
            matrix = NULL;
        } else {
            for (int i = 0; i < rows; i++) {
                *(matrix + i) = data + i * cols;
            }
        }
    }

    return matrix;
}

int **allocate_dynamic_3(int rows, int cols) {
    int **matrix = malloc(rows * sizeof(int *) + rows * cols * sizeof(int));

    if (matrix != NULL) {
        int *data = (int *)(matrix + rows);
        for (int i = 0; i < rows; i++) {
            *(matrix + i) = data + i * cols;
        }
    }

    return matrix;
}

void free_matrix(int **matrix, int type, int rows) {
    if (matrix != NULL) {
        if (type == 2) {
            for (int i = 0; i < rows; i++) {
                free(*(matrix + i));
            }
            free(matrix);
        } else if (type == 3) {
            free(*matrix);
            free(matrix);
        } else if (type == 4) {
            free(matrix);
        }
    }
}


#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100

int input_size(int *rows, int *cols);
int input_matrix(int **matrix, int rows, int cols);
void output_matrix(int **matrix, int rows, int cols);
void output_array(int *array, int size);
int **allocate_dynamic_1(int rows, int cols);
int **allocate_dynamic_2(int rows, int cols);
int **allocate_dynamic_3(int rows, int cols);
void free_matrix(int **matrix, int type, int rows);
void find_row_max(int **matrix, int rows, int cols, int *result);
void find_col_min(int **matrix, int rows, int cols, int *result);

int main(void) {
    int type, rows, cols;
    int static_matrix[MAX_SIZE][MAX_SIZE];
    int *static_ptrs[MAX_SIZE];
    int row_max[MAX_SIZE];
    int col_min[MAX_SIZE];
    int **matrix = NULL;

    if (scanf("%d", &type) != 1 || type < 1 || type > 4 || !input_size(&rows, &cols)) {
        printf("n/a");
        return 1;
    }

    if (type == 1) {
        if (rows > MAX_SIZE || cols > MAX_SIZE) {
            printf("n/a");
            return 1;
        }
        for (int i = 0; i < rows; i++) {
            static_ptrs[i] = static_matrix[i];
        }
        matrix = static_ptrs;
    } else if (type == 2) {
        matrix = allocate_dynamic_1(rows, cols);
    } else if (type == 3) {
        matrix = allocate_dynamic_2(rows, cols);
    } else {
        matrix = allocate_dynamic_3(rows, cols);
    }

    if (matrix == NULL || !input_matrix(matrix, rows, cols)) {
        printf("n/a");
        free_matrix(matrix, type, rows);
        return 1;
    }

    find_row_max(matrix, rows, cols, row_max);
    find_col_min(matrix, rows, cols, col_min);

    output_matrix(matrix, rows, cols);
    printf("\n");
    output_array(row_max, rows);
    printf("\n");
    output_array(col_min, cols);

    free_matrix(matrix, type, rows);

    return 0;
}

int input_size(int *rows, int *cols) {
    return scanf("%d %d", rows, cols) == 2 && *rows > 0 && *cols > 0;
}

int input_matrix(int **matrix, int rows, int cols) {
    int result = 1;

    for (int i = 0; result && i < rows; i++) {
        for (int j = 0; result && j < cols; j++) {
            if (scanf("%d", *(matrix + i) + j) != 1) {
                result = 0;
            }
        }
    }

    return result;
}

void output_matrix(int **matrix, int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        if (i > 0) {
            printf("\n");
        }
        for (int j = 0; j < cols; j++) {
            if (j > 0) {
                printf(" ");
            }
            printf("%d", *(*(matrix + i) + j));
        }
    }
}

void output_array(int *array, int size) {
    for (int i = 0; i < size; i++) {
        if (i > 0) {
            printf(" ");
        }
        printf("%d", *(array + i));
    }
}

int **allocate_dynamic_1(int rows, int cols) {
    int **matrix = malloc(rows * sizeof(int *));

    if (matrix != NULL) {
        for (int i = 0; i < rows; i++) {
            *(matrix + i) = malloc(cols * sizeof(int));
            if (*(matrix + i) == NULL) {
                for (int j = 0; j < i; j++) {
                    free(*(matrix + j));
                }
                free(matrix);
                matrix = NULL;
                break;
            }
        }
    }

    return matrix;
}

int **allocate_dynamic_2(int rows, int cols) {
    int **matrix = malloc(rows * sizeof(int *));
    int *data = NULL;

    if (matrix != NULL) {
        data = malloc(rows * cols * sizeof(int));
        if (data == NULL) {
            free(matrix);
            matrix = NULL;
        } else {
            for (int i = 0; i < rows; i++) {
                *(matrix + i) = data + i * cols;
            }
        }
    }

    return matrix;
}

int **allocate_dynamic_3(int rows, int cols) {
    int **matrix = malloc(rows * sizeof(int *) + rows * cols * sizeof(int));

    if (matrix != NULL) {
        int *data = (int *)(matrix + rows);
        for (int i = 0; i < rows; i++) {
            *(matrix + i) = data + i * cols;
        }
    }

    return matrix;
}

void free_matrix(int **matrix, int type, int rows) {
    if (matrix != NULL) {
        if (type == 2) {
            for (int i = 0; i < rows; i++) {
                free(*(matrix + i));
            }
            free(matrix);
        } else if (type == 3) {
            free(*matrix);
            free(matrix);
        } else if (type == 4) {
            free(matrix);
        }
    }
}

void find_row_max(int **matrix, int rows, int cols, int *result) {
    for (int i = 0; i < rows; i++) {
        *(result + i) = *(*(matrix + i));
        for (int j = 1; j < cols; j++) {
            if (*(*(matrix + i) + j) > *(result + i)) {
                *(result + i) = *(*(matrix + i) + j);
            }
        }
    }
}

void find_col_min(int **matrix, int rows, int cols, int *result) {
    for (int j = 0; j < cols; j++) {
        *(result + j) = **matrix;
        for (int i = 0; i < rows; i++) {
            if (i == 0 || *(*(matrix + i) + j) < *(result + j)) {
                *(result + j) = *(*(matrix + i) + j);
            }
        }
    }
}


#include <stdio.h>

#define N 15
#define M 13

void transform(int *buf, int **matr, int n, int m);
void make_picture(int **picture, int n, int m);
void reset_picture(int **picture, int n, int m);
void output_picture(int **picture, int n, int m);
void draw_horizontal(int **picture, int row, int *data, int length);
void draw_vertical(int **picture, int col, int *data, int length);
void draw_sun(int **picture, int row, int col, int sun_data[6][5]);

int main(void) {
    int picture_data[N][M];
    int *picture[N];

    transform((int *)picture_data, picture, N, M);
    make_picture(picture, N, M);
    output_picture(picture, N, M);

    return 0;
}

void make_picture(int **picture, int n, int m) {
    int frame_w[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
    int frame_h[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
    int tree_trunk[] = {7, 7, 7, 7};
    int tree_foliage[] = {3, 3, 3, 3};
    int sun_data[6][5] = {{0, 6, 6, 6, 6}, {0, 0, 6, 6, 6}, {0, 0, 6, 6, 6},
                          {0, 6, 0, 0, 6}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}};

    reset_picture(picture, n, m);

    draw_horizontal(picture, 0, frame_w, M);
    draw_horizontal(picture, 7, frame_w, M);
    draw_horizontal(picture, 14, frame_w, M);

    draw_vertical(picture, 0, frame_h, N);
    draw_vertical(picture, 6, frame_h, N);
    draw_vertical(picture, 12, frame_h, N);

    draw_sun(picture, 1, 7, sun_data);

    draw_horizontal(picture, 3, tree_foliage, 4);
    draw_horizontal(picture, 4, tree_foliage, 4);
    picture[2][3] = 3;
    picture[2][4] = 3;
    picture[5][3] = 3;
    picture[5][4] = 3;

    picture[6][3] = tree_trunk[0];
    picture[6][4] = tree_trunk[1];
    picture[8][3] = tree_trunk[2];
    picture[8][4] = tree_trunk[3];
    picture[9][3] = 7;
    picture[9][4] = 7;
    draw_horizontal(picture, 10, tree_trunk, 4);
}

void reset_picture(int **picture, int n, int m) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            picture[i][j] = 0;
        }
    }
}

void transform(int *buf, int **matr, int n, int m) {
    for (int i = 0; i < n; i++) {
        matr[i] = buf + i * m;
    }
}

void output_picture(int **picture, int n, int m) {
    for (int i = 0; i < n; i++) {
        if (i > 0) {
            printf("\n");
        }
        for (int j = 0; j < m; j++) {
            if (j > 0) {
                printf(" ");
            }
            printf("%d", picture[i][j]);
        }
    }
}

void draw_horizontal(int **picture, int row, int *data, int length) {
    int start_col = 0;

    if (length == 4) {
        start_col = 2;
    }

    for (int i = 0; i < length; i++) {
        picture[row][start_col + i] = data[i];
    }
}

void draw_vertical(int **picture, int col, int *data, int length) {
    for (int i = 0; i < length; i++) {
        picture[i][col] = data[i];
    }
}

void draw_sun(int **picture, int row, int col, int sun_data[6][5]) {
    for (int i = 0; i < 6; i++) {
        for (int j = 0; j < 5; j++) {
            picture[row + i][col + j] = sun_data[i][j];
        }
    }
}