#include <stdio.h>
#include <stdlib.h>
/* 函数声明 */
int** Input(int*** A, int*** B, int* m, int* n);
int** Combine(int** A, int** B, int m, int n, int* p);
void Area(int** C, int p);
void Print(int** C, int p);
int main(void) {
int m, n, **A, **B, **C, p;
A = Input(&A, &B, &m, &n);
C = Combine(A, B, m, n, &p);
Print(C, p);
Area(C, p);
// 释放分配的内存
for (int i = 0; i < 2; i++) {
free(A[i]);
free(B[i]);
free(C[i]);
}
free(A);
free(B);
free(C);
return 0;
}
/* 从键盘输入两个二维数组 */
int** Input(int*** A, int*** B, int* m, int* n) {
scanf("%d", m);
*A = (int**)malloc(2 * sizeof(int*));
(*A)[0] = (int*)malloc(*m * sizeof(int));
(*A)[1] = (int*)malloc(*m * sizeof(int));
for (int i = 0; i < *m; i++) {
scanf("%d %d", &((*A)[0][i]), &((*A)[1][i]));
}
scanf("%d", n);
*B = (int**)malloc(2 * sizeof(int*));
(*B)[0] = (int*)malloc(*n * sizeof(int));
(*B)[1] = (int*)malloc(*n * sizeof(int));
for (int i = 0; i < *n; i++) {
scanf("%d %d", &((*B)[0][i]), &((*B)[1][i]));
}
return *A;
}
/* 合并两个二维数组 */
int** Combine(int** A, int** B, int m, int n, int* p) {
*p = 0;
int** C = (int**)malloc(2 * sizeof(int*));
for (int i = 0; i < 2; i++) {
C[i] = (int*)malloc((m + n) * sizeof(int));
}
int i = 0, j = 0;
while (i < m && j < n) {
if (A[0][i] < B[0][j]) {
C[0][*p] = A[0][i];
C[1][*p] = A[1][i];
i++;
} else if (A[0][i] > B[0][j]) {
C[0][*p] = B[0][j];
C[1][*p] = B[1][j];
j++;
} else {
C[0][*p] = A[0][i];
C[1][*p] = (A[1][i] + B[1][j]) / 2;
i++;
j++;
}
(*p)++;
}
while (i < m) {
C[0][*p] = A[0][i];
C[1][*p] = A[1][i];
i++;
(*p)++;
}
while (j < n) {
C[0][*p] = B[0][j];
C[1][*p] = B[1][j];
j++;
(*p)++;
}
return C;
}
/* 计算二维数组的面积 */
void Area(int** C, int p) {
double area = 0.0;
for (int i = 1; i < p; i++) {
area += (C[1][i] + C[1][i-1]) * (C[0][i] - C[0][i-1]) / 2.0;
}
printf("合并后的数组面积: %.2f\n", area);
}
/* 输出二维数组 */
void Print(int** C, int p) {
printf("[合并后的数组输出]\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < p; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCi8qIOWHveaVsOWjsOaYjiAqLwppbnQqKiBJbnB1dChpbnQqKiogQSwgaW50KioqIEIsIGludCogbSwgaW50KiBuKTsKaW50KiogQ29tYmluZShpbnQqKiBBLCBpbnQqKiBCLCBpbnQgbSwgaW50IG4sIGludCogcCk7CnZvaWQgQXJlYShpbnQqKiBDLCBpbnQgcCk7CnZvaWQgUHJpbnQoaW50KiogQywgaW50IHApOwoKaW50IG1haW4odm9pZCkgewogICAgaW50IG0sIG4sICoqQSwgKipCLCAqKkMsIHA7CiAgICBBID0gSW5wdXQoJkEsICZCLCAmbSwgJm4pOwogICAgQyA9IENvbWJpbmUoQSwgQiwgbSwgbiwgJnApOwogICAgUHJpbnQoQywgcCk7CiAgICBBcmVhKEMsIHApOwogICAgLy8g6YeK5pS+5YiG6YWN55qE5YaF5a2YCiAgICBmb3IgKGludCBpID0gMDsgaSA8IDI7IGkrKykgewogICAgICAgIGZyZWUoQVtpXSk7CiAgICAgICAgZnJlZShCW2ldKTsKICAgICAgICBmcmVlKENbaV0pOwogICAgfQogICAgZnJlZShBKTsKICAgIGZyZWUoQik7CiAgICBmcmVlKEMpOwogICAgcmV0dXJuIDA7Cn0KCi8qIOS7jumUruebmOi+k+WFpeS4pOS4quS6jOe7tOaVsOe7hCAqLwppbnQqKiBJbnB1dChpbnQqKiogQSwgaW50KioqIEIsIGludCogbSwgaW50KiBuKSB7CiAgICBzY2FuZigiJWQiLCBtKTsKICAgICpBID0gKGludCoqKW1hbGxvYygyICogc2l6ZW9mKGludCopKTsKICAgICgqQSlbMF0gPSAoaW50KiltYWxsb2MoKm0gKiBzaXplb2YoaW50KSk7CiAgICAoKkEpWzFdID0gKGludCopbWFsbG9jKCptICogc2l6ZW9mKGludCkpOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCAqbTsgaSsrKSB7CiAgICAgICAgc2NhbmYoIiVkICVkIiwgJigoKkEpWzBdW2ldKSwgJigoKkEpWzFdW2ldKSk7CiAgICB9CiAgICBzY2FuZigiJWQiLCBuKTsKICAgICpCID0gKGludCoqKW1hbGxvYygyICogc2l6ZW9mKGludCopKTsKICAgICgqQilbMF0gPSAoaW50KiltYWxsb2MoKm4gKiBzaXplb2YoaW50KSk7CiAgICAoKkIpWzFdID0gKGludCopbWFsbG9jKCpuICogc2l6ZW9mKGludCkpOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCAqbjsgaSsrKSB7CiAgICAgICAgc2NhbmYoIiVkICVkIiwgJigoKkIpWzBdW2ldKSwgJigoKkIpWzFdW2ldKSk7CiAgICB9CiAgICByZXR1cm4gKkE7Cn0KCi8qIOWQiOW5tuS4pOS4quS6jOe7tOaVsOe7hCAqLwppbnQqKiBDb21iaW5lKGludCoqIEEsIGludCoqIEIsIGludCBtLCBpbnQgbiwgaW50KiBwKSB7CiAgICAqcCA9IDA7CiAgICBpbnQqKiBDID0gKGludCoqKW1hbGxvYygyICogc2l6ZW9mKGludCopKTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgMjsgaSsrKSB7CiAgICAgICAgQ1tpXSA9IChpbnQqKW1hbGxvYygobSArIG4pICogc2l6ZW9mKGludCkpOwogICAgfQogICAgaW50IGkgPSAwLCBqID0gMDsKICAgIHdoaWxlIChpIDwgbSAmJiBqIDwgbikgewogICAgICAgIGlmIChBWzBdW2ldIDwgQlswXVtqXSkgewogICAgICAgICAgICBDWzBdWypwXSA9IEFbMF1baV07CiAgICAgICAgICAgIENbMV1bKnBdID0gQVsxXVtpXTsKICAgICAgICAgICAgaSsrOwogICAgICAgIH0gZWxzZSBpZiAoQVswXVtpXSA+IEJbMF1bal0pIHsKICAgICAgICAgICAgQ1swXVsqcF0gPSBCWzBdW2pdOwogICAgICAgICAgICBDWzFdWypwXSA9IEJbMV1bal07CiAgICAgICAgICAgIGorKzsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBDWzBdWypwXSA9IEFbMF1baV07CiAgICAgICAgICAgIENbMV1bKnBdID0gKEFbMV1baV0gKyBCWzFdW2pdKSAvIDI7CiAgICAgICAgICAgIGkrKzsKICAgICAgICAgICAgaisrOwogICAgICAgIH0KICAgICAgICAoKnApKys7CiAgICB9CiAgICB3aGlsZSAoaSA8IG0pIHsKICAgICAgICBDWzBdWypwXSA9IEFbMF1baV07CiAgICAgICAgQ1sxXVsqcF0gPSBBWzFdW2ldOwogICAgICAgIGkrKzsKICAgICAgICAoKnApKys7CiAgICB9CiAgICB3aGlsZSAoaiA8IG4pIHsKICAgICAgICBDWzBdWypwXSA9IEJbMF1bal07CiAgICAgICAgQ1sxXVsqcF0gPSBCWzFdW2pdOwogICAgICAgIGorKzsKICAgICAgICAoKnApKys7CiAgICB9CiAgICByZXR1cm4gQzsKfQoKLyog6K6h566X5LqM57u05pWw57uE55qE6Z2i56evICovCnZvaWQgQXJlYShpbnQqKiBDLCBpbnQgcCkgewogICAgZG91YmxlIGFyZWEgPSAwLjA7CiAgICBmb3IgKGludCBpID0gMTsgaSA8IHA7IGkrKykgewogICAgICAgIGFyZWEgKz0gKENbMV1baV0gKyBDWzFdW2ktMV0pICogKENbMF1baV0gLSBDWzBdW2ktMV0pIC8gMi4wOwogICAgfQogICAgcHJpbnRmKCLlkIjlubblkI7nmoTmlbDnu4TpnaLnp686ICUuMmZcbiIsIGFyZWEpOwp9CgovKiDovpPlh7rkuoznu7TmlbDnu4QgKi8Kdm9pZCBQcmludChpbnQqKiBDLCBpbnQgcCkgewogICAgcHJpbnRmKCJb5ZCI5bm25ZCO55qE5pWw57uE6L6T5Ye6XVxuIik7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IDI7IGkrKykgewogICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgcDsgaisrKSB7CiAgICAgICAgICAgIHByaW50ZigiJWQgIiwgQ1tpXVtqXSk7CiAgICAgICAgfQogICAgICAgIHByaW50ZigiXG4iKTsKICAgIH0KfQo=