fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* 函数声明 */
  5. int** Input(int*** A, int*** B, int* m, int* n);
  6. int** Combine(int** A, int** B, int m, int n, int* p);
  7. void Area(int** C, int p);
  8. void Print(int** C, int p);
  9.  
  10. int main(void) {
  11. int m, n, **A, **B, **C, p;
  12. A = Input(&A, &B, &m, &n);
  13. C = Combine(A, B, m, n, &p);
  14. Print(C, p);
  15. Area(C, p);
  16. // 释放分配的内存
  17. for (int i = 0; i < 2; i++) {
  18. free(A[i]);
  19. free(B[i]);
  20. free(C[i]);
  21. }
  22. free(A);
  23. free(B);
  24. free(C);
  25. return 0;
  26. }
  27.  
  28. /* 从键盘输入两个二维数组 */
  29. int** Input(int*** A, int*** B, int* m, int* n) {
  30. scanf("%d", m);
  31. *A = (int**)malloc(2 * sizeof(int*));
  32. (*A)[0] = (int*)malloc(*m * sizeof(int));
  33. (*A)[1] = (int*)malloc(*m * sizeof(int));
  34. for (int i = 0; i < *m; i++) {
  35. scanf("%d %d", &((*A)[0][i]), &((*A)[1][i]));
  36. }
  37. scanf("%d", n);
  38. *B = (int**)malloc(2 * sizeof(int*));
  39. (*B)[0] = (int*)malloc(*n * sizeof(int));
  40. (*B)[1] = (int*)malloc(*n * sizeof(int));
  41. for (int i = 0; i < *n; i++) {
  42. scanf("%d %d", &((*B)[0][i]), &((*B)[1][i]));
  43. }
  44. return *A;
  45. }
  46.  
  47. /* 合并两个二维数组 */
  48. int** Combine(int** A, int** B, int m, int n, int* p) {
  49. *p = 0;
  50. int** C = (int**)malloc(2 * sizeof(int*));
  51. for (int i = 0; i < 2; i++) {
  52. C[i] = (int*)malloc((m + n) * sizeof(int));
  53. }
  54. int i = 0, j = 0;
  55. while (i < m && j < n) {
  56. if (A[0][i] < B[0][j]) {
  57. C[0][*p] = A[0][i];
  58. C[1][*p] = A[1][i];
  59. i++;
  60. } else if (A[0][i] > B[0][j]) {
  61. C[0][*p] = B[0][j];
  62. C[1][*p] = B[1][j];
  63. j++;
  64. } else {
  65. C[0][*p] = A[0][i];
  66. C[1][*p] = (A[1][i] + B[1][j]) / 2;
  67. i++;
  68. j++;
  69. }
  70. (*p)++;
  71. }
  72. while (i < m) {
  73. C[0][*p] = A[0][i];
  74. C[1][*p] = A[1][i];
  75. i++;
  76. (*p)++;
  77. }
  78. while (j < n) {
  79. C[0][*p] = B[0][j];
  80. C[1][*p] = B[1][j];
  81. j++;
  82. (*p)++;
  83. }
  84. return C;
  85. }
  86.  
  87. /* 计算二维数组的面积 */
  88. void Area(int** C, int p) {
  89. double area = 0.0;
  90. for (int i = 1; i < p; i++) {
  91. area += (C[1][i] + C[1][i-1]) * (C[0][i] - C[0][i-1]) / 2.0;
  92. }
  93. printf("合并后的数组面积: %.2f\n", area);
  94. }
  95.  
  96. /* 输出二维数组 */
  97. void Print(int** C, int p) {
  98. printf("[合并后的数组输出]\n");
  99. for (int i = 0; i < 2; i++) {
  100. for (int j = 0; j < p; j++) {
  101. printf("%d ", C[i][j]);
  102. }
  103. printf("\n");
  104. }
  105. }
  106.  
Success #stdin #stdout 0s 5276KB
stdin
4
1 3
4 5
7 4
9 2
5
2 3
3 5
4 4
5 2
8 1
stdout
[合并后的数组输出]
1 2 3 4 5 7 8 9 
3 3 5 4 2 4 1 2 
合并后的数组面积: 24.50