first common element
// 给定两个递增有序表,输出两表第一个相同的元素
#include <stdio.h>
#include <string.h>
int *fst_common_val(int *a,int an, int *b, int bn)
{
int *ptra, *ptrb;
ptra = a; ptrb = b;
while(ptra < a+an && ptrb < b + bn)
{
if(*ptra < *ptrb) ptra ++;
else if(*ptra > *ptrb) ptrb++;
else
return ptra;
}
return ptra;
}
int main()
{
int *a[] = {1,3,5,7,9,11,13,17,19};
int *b[] = {2,4,6,8,11,56,};
int *value;
int (*func)();
func = fst_common_val;
printf("the elements of a are: \n");
for(int i=0; i<sizeof(a)/sizeof(a[0]); i++)
printf("%d ", *(a+i));
printf("\nthe elements of b are: \n");
for(int i=0; i<sizeof(b)/sizeof(b[1]); i++)
printf("%d ", *(b+i));
value = (*func)(a,sizeof(a)/sizeof(a[0]), b, sizeof(b)/ sizeof(b[0]));
printf("\nthe fist common element is : %d", *value);
return 0;
}
代码是强行用指针的,为了练习指针的使用,不是最优解
来源:CSDN
作者:ever_promise
链接:https://blog.csdn.net/ever_promise/article/details/104268866