C程序 查找数组中常见元素
在这里,我们将看到如何用C语言程序找到常见的数组元素。
输入:
a[6] = {1,2,3,4,5,6}
b[6] = {5,6,7,8,9,10}
输出:
common array elements is 5 6
// C program to find the common array elements
#include <stdio.h>
int main()
{
int a[6] = { 1, 2, 3, 4, 5, 6 };
int b[6] = { 5, 6, 7, 8, 9, 10 };
int c[10], i, j, k = 0, x, count;
for (i = 0; i < 7; i++) {
for (j = 0; j < 7; j++) {
// checking if element in
// array 1 is equal to
// the element in array 2
if (a[i] == b[j]) {
count = 0;
for (x = 0; x < k; x++) {
if (a[i] == c[x])
count++;
}
// storing common elements in c array
if (count == 0) {
c[k] = a[i];
// incrementing the k value to store the
// size of array 3
k++;
}
}
}
}
printf("Common array elements is \n");
for (i = 0; i < k; i++)
// printing the result
printf("%d ", c[i]);
return 0;
}
输出
common array elements is
5 6