c语言如何判断一个数组里的数是否在另一个数组里出现
时间: 2023-10-07 18:07:40 浏览: 166
可以使用两个循环遍历两个数组,对于第一个数组中的每个元素,在第二个数组中查找是否有相同的元素。如果找到了相同的元素,即可判断第一个数组中的元素在第二个数组中出现过。
以下是一个示例代码:
```c
#include <stdio.h>
int main() {
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {3, 5, 7, 9, 11};
int length1 = sizeof(arr1) / sizeof(int);
int length2 = sizeof(arr2) / sizeof(int);
for (int i = 0; i < length1; i++) {
int found = 0;
for (int j = 0; j < length2; j++) {
if (arr1[i] == arr2[j]) {
found = 1;
break;
}
}
if (found) {
printf("%d is in the second array.\n", arr1[i]);
} else {
printf("%d is not in the second array.\n", arr1[i]);
}
}
return 0;
}
```
输出结果:
```
1 is not in the second array.
2 is not in the second array.
3 is in the second array.
4 is not in the second array.
5 is in the second array.
```