用C语言比较两个二维数组并计算交并比
时间: 2023-05-20 10:05:01 浏览: 79
c语言实现二维数据求和
以下是用C语言比较两个二维数组并计算交并比的示例代码:
```c
#include <stdio.h>
#define ROWS 3
#define COLS 3
int main() {
int arr1[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int arr2[ROWS][COLS] = {{2, 3, 4}, {5, 6, 7}, {8, 9, 10}};
int intersection = 0;
int union_set[ROWS * COLS];
int union_size = 0;
// Compare the two arrays and calculate intersection and union
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (arr1[i][j] == arr2[i][j]) {
intersection++;
union_set[union_size++] = arr1[i][j];
} else {
union_set[union_size++] = arr1[i][j];
union_set[union_size++] = arr2[i][j];
}
}
}
// Print the results
printf("Intersection: %d\n", intersection);
printf("Union: ");
for (int i = 0; i < union_size; i++) {
printf("%d ", union_set[i]);
}
printf("\n");
return 0;
}
```
输出结果为:
```
Intersection: 2
Union: 1 2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9 10
```
其中,intersection 表示交集的大小,union_set 表示并集的元素集合,union_size 表示并集的大小。
阅读全文