c语言思想20
来源:互联网 发布:淘宝店铺流量软件app 编辑:程序博客网 时间:2024/06/14 10:36
/*[函数]在已知两个从小到大有序的数表中寻找都出现的第一个元素的指针*/
#include<stdio.h>
#include<stdlib.h>
#define NULL 0int *search2(int *pa,int *pb,int an,int bn)
{
int *ca,*cb;
ca=pa;cb=pb;/*为ca、cb设定初值*/
while(ca<pa+an&&cb<pb+bn)/*两个数表都未考察完*/ 数组与指针发生联系一般默认为指针px=数组[0]
{
/*在两个数表中找下一个相等的元素*/
if(*ca<*cb)/*数表1的当前元素<数表2的当前元素*/
ca++;/*调整数表1的当前元素指针*/
else if(*ca>*cb)/*数表1的当前元素>数表2的当前元素*/
cb++;/*调整数表2的当前元素指针*/
else /*数表1的当前元素==数表2的当前元素*/
/*在前两个数表中找到相等元素*/
return ca;/*返回在这两个数表中找到相等元素*/
}
return NULL;
}
main( )/*只是为了引用函数search2( )*/
{
int *vp,i;
int a[ ]={1,3,5,7,9,13,15,27,29,37};
int b[ ]={2,4,6,8,10,13,14,27,29,37};
clrscr();
puts("The elements of array a is:");
for(i=0;i<sizeof(a)/sizeof(a[0]);i++) 打印数组for(i;比较; i++)
printf(" %d",a[i]);
puts("\nThe elements of array b is:");
for(i=0;i<sizeof(b)/sizeof(b[0]);i++)
printf(" %d",b[i]);
vp=search2(a,b,sizeof a/sizeof a[0],sizeof b/sizeof b[0]);
if(vp) printf("\nThe first same number in both arrays is %d\n",*vp);
else printf("Not found!\n");
puts("\n Press any key to quit...\n");
getchar();
getchar();
}
0 0
- c语言思想20
- c语言思想20
- c语言模块化思想
- c语言思想3
- c语言思想4
- C语言思想5
- C语言思想6
- c语言思想7
- c语言思想8
- c语言思想9
- c语言思想10
- c语言思想11
- c语言思想12
- c语言思想13
- C语言思想14
- c语言思想15
- c语言思想16
- c语言思想17
- 构造函数与析构函数
- 单片机通信方式——串行通信(同步、异步)、并行通信
- java遍历HashMap的方法
- AC自动机构造字符串(好)poj2778
- poj 1416 组合数(DIY碎纸机)
- c语言思想20
- 记录: android:autoLink 属性 -自动补全AutoCompleteTextView
- POJ 2983 Is the Information Reliable?(差分约束系统)
- Esper事件处理引擎_16_EPL 语法_8_NamedWindow
- Minimum Cost
- hdu 4366 Card Collector(组合数学:容斥定理)
- 011实现一个栈,除了push和pop操作,还要实现min函数以返回栈中的最小值,时间复杂度都为O(1)(keep it up)
- ios 点击空白处 收回键盘
- Convert Sorted Array to Binary Search Tree 将有序数组转化为平衡二叉排序树(重重)