占个位
程序代码:#include
#include
#define NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))
int numeric (const int *p1, const int *p2)
{
return(*p1 - *p2);
}
void Bsearch(int a[],int num)
{
int *itemptr;
/* The cast of (int(*)(const void *,const void*))
is needed to avoid a type mismatch error at
compile time */
itemptr = bsearch (&num, a, NELEMS(a), sizeof(int), (int(*)(const void *,const void *))numeric);
if (!itemptr)
printf("%d is in the table.\n",num);
else
printf("%d isn't in the table.\n",num);
}
void BinarySearch(int a[],int v,int r)
{
int m;
int l=0;
r=r-1;
while(l<=r)
{
m=(l+r)/2;
if(v==a[m])
{
printf("Found! The search number's location is: %d\n",m);
return 0;
}
if(v
r=m-1;
else
l=m+1;
}
printf("Not found!\n");
}
void EnhancedSqSearch(int a[],int v,int r)
{
int i=0;
a[10]=v;
while(a[i]!=v)
i++;
if(i
printf("Found! The search number's location is: %d\n",i);
else
printf("Not found!\n");
}
void SequentialSearch(int a[],int v,int r)
{
int i;
for(i=0;i
{
if(v==a[i])
{
printf("Found! The search number's location is: %d\n",i);
return 0;
}
}
printf("Not found!\n");
}
main()
{
int a[11];
int i,num;
printf("Please input ten number: \n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("Please input your search number: \n");
scanf("%d",&num);
printf("1):********\n");
SequentialSearch(a,num,10);//顺序查找
printf("2):********\n");
EnhancedSqSearch(a,num,10);//增强顺序查找
printf("3):********\n");
BinarySearch(a,num,10);//二分查找
printf("4):********\n");
Bsearch(a,num);//库函数 Bsearch 只知道有这个东西 我不熟悉
}
[[it] 本帖最后由 新浪 于 2008-11-15 14:21 编辑 [/it]]