有15个数存放在一个数组中,输入一个数,要求用折半查找法找出该数是数组中第几个元素的值 5
如果该数不在数组中,则输出“无此数”。以15个数用赋初值的方法在程序中给出。要找的数用scanf函数输入。赶交作业,求快答...
如果该数不在数组中,则输出“无此数”。以15个数用赋初值的方法在程序中给出。要找的数用scanf函数输入。
赶交作业,求快答 展开
赶交作业,求快答 展开
4个回答
展开全部
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int ary[15]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int num = 16;
int pos;
int low;
int high;
int mid;
low = 1;
high = 15;
pos = 0;
while(high>=low){
mid = (low + high)/2;
if(ary[mid-1]>num){
high = mid-1;
}else if(ary[mid-1]<num){
low = mid + 1;
}else{
pos = mid;
break;
}
}
if(pos == 0){
printf("the data is not found\n");
}else{
printf("the data pos is :%d\n",pos);
}
return 0;
}
#include <stdlib.h>
int main(void) {
int ary[15]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int num = 16;
int pos;
int low;
int high;
int mid;
low = 1;
high = 15;
pos = 0;
while(high>=low){
mid = (low + high)/2;
if(ary[mid-1]>num){
high = mid-1;
}else if(ary[mid-1]<num){
low = mid + 1;
}else{
pos = mid;
break;
}
}
if(pos == 0){
printf("the data is not found\n");
}else{
printf("the data pos is :%d\n",pos);
}
return 0;
}
展开全部
//希望对你又帮助,这题的方法很多,只有自己多去思考。祝你有所进步!
#include<stdio.h>
void main()
{
int a[10]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int low=0,mid,high=15,b,i=-1;
printf("请输入你要查找到数: ");
scanf("%d",&b);
do{if(b==a[15])
{
i=14;break;
}
mid=(low+high)/2;
if(b<a[mid])
high=mid;
else
if(b>a[mid])
low=mid;
else
i=mid;
}while(b!=a[mid]);
if(i==-1)
printf("该数不在数组中\n");
else
printf("该数在数组中的位置为%d",i+1);
}
#include<stdio.h>
void main()
{
int a[10]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int low=0,mid,high=15,b,i=-1;
printf("请输入你要查找到数: ");
scanf("%d",&b);
do{if(b==a[15])
{
i=14;break;
}
mid=(low+high)/2;
if(b<a[mid])
high=mid;
else
if(b>a[mid])
low=mid;
else
i=mid;
}while(b!=a[mid]);
if(i==-1)
printf("该数不在数组中\n");
else
printf("该数在数组中的位置为%d",i+1);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
折半查找算法
注:数组内元素必须按从小到大顺序排列!
int halfsearch(int* number,int first,int last,int av)
{
if(first<last)
{
int i=(first+last)/2;
if(number[i]==av)
{
return i;
}
else if(av<number[i])
{
halfsearch(number,first,i-1,av);
}
else if(av>number[i])
{
halfsearch(number,i+1,last,av);
}
}
else if(first==last)
{
return (number[first]==av?first:0);
}
else
{
return 0;
}
};
注:数组内元素必须按从小到大顺序排列!
int halfsearch(int* number,int first,int last,int av)
{
if(first<last)
{
int i=(first+last)/2;
if(number[i]==av)
{
return i;
}
else if(av<number[i])
{
halfsearch(number,first,i-1,av);
}
else if(av>number[i])
{
halfsearch(number,i+1,last,av);
}
}
else if(first==last)
{
return (number[first]==av?first:0);
}
else
{
return 0;
}
};
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
include <stdio.h>
int BinSearch(int* r,int n,int k)
{
int low=0,high=n-1,mid;
while(low<=high)
{
if(r[low]==k)
return low;
if(r[high]==k)
return high;
mid=low+((high-low)/2);
if(r[mid]==k)
return mid;
if(r[mid]<k)
low=mid+1;//继续在R[mid+1..high]中查找
else
high=mid-1;//继续在R[low..mid-1]中查找
}
if(low>high)
return -1;//当low>high时表示所查找区间内没有结果,查找失败
}
int mian()
{
int a[15]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14};
int key,result;
printf("请输入要查找的数\n");
scanf("%d",&key);
result=BinSearch(a,15,key);
if(result!=-1)
printf("%d是第%d个元素\n",key,result);
else
printf("无此数\n");
return 0;
}
int BinSearch(int* r,int n,int k)
{
int low=0,high=n-1,mid;
while(low<=high)
{
if(r[low]==k)
return low;
if(r[high]==k)
return high;
mid=low+((high-low)/2);
if(r[mid]==k)
return mid;
if(r[mid]<k)
low=mid+1;//继续在R[mid+1..high]中查找
else
high=mid-1;//继续在R[low..mid-1]中查找
}
if(low>high)
return -1;//当low>high时表示所查找区间内没有结果,查找失败
}
int mian()
{
int a[15]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14};
int key,result;
printf("请输入要查找的数\n");
scanf("%d",&key);
result=BinSearch(a,15,key);
if(result!=-1)
printf("%d是第%d个元素\n",key,result);
else
printf("无此数\n");
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询