对数组元素的一些简单操作
求两个有序数组的公共元素
#include<stdio.h>
#include<stdlib.h>
#define N 7
#define M 5
void intersection(int array1[], int array2[])
{
int i = 0,j = 0;
while (i < M&&j < N)
{
while (array1[i] < array2[j])
i++;
while (array1[i] > array2[j])
j++;
while (array1[i] == array2[j])
{
printf("%d ", array1[i]);
i++, j++;
}
}
printf("\n");
}
int main()
{
int array1[M] = {
1,3,4,5,8};
int array2[N] = { 2, 3, 5, 6, 7, 8, 11};
intersection(array1, array2);
system("pause");
return 0;
}
求三个有序数组的公共元素
#include<stdio.h>
#include<stdlib.h>
#define N 7
void intersection(int array1[], int array2[],int b[])
{
int i = 0, j = 0,k = 0;
while (i < N&&j < N)
{
while (array1[i] < array2[j] && i<N)
i++;
while (array1[i] > array2[j] && j<N)
j++;
while (array1[i] == array2[j] && i<N&&j<N)
{
b[k++] = array1[i];
i++, j++;
}
}
}
int main()
{
int i;
int a1[N] = {