C语言 二分查找算法 用递归实现 我改动了一下
#include<stdio.h>intRecorBinarySearch(inta[],int,int,int,int,int,int);intmain(void){i...
#include<stdio.h>
int RecorBinarySearch(int a[],int,int,int,int,int,int);
int main(void)
{
int n=0,m=0;int a[10000],b[10000],c[10000];int low;int high;int mid;
printf("");
scanf("%d",&n);
printf("");
scanf("%d",&m);
printf("");
int i=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("");
for(i=0;i<m;i++)
{
scanf("%d",&b);
c[i]=RecorBinarySearch(a[i],i,n,b,low,high,mid);
}
printf("");
for(i=0;i<m;i++)
{
printf("%d",c[i]);
}
freopen("in.in","r",stdin);
freopen("out.out","w",stdout);
return 0;
}
int RecorBinarySearch(int a[],int i,int n,int key,int low,int high,int mid)
{
low=0;high=n-1;mid=low+(high-low)/2;
if(low>high)
return -1;
else
{
if(a[i]>a[mid])
{
low=mid;
RecorBinarySearch(a,i,n,key,low,high,mid+1);
}
if(a[i]==a[mid])
{
return mid;
}
if(a[i]<a[mid])
{
high=mid;
RecorBinarySearch(a,i,n,key,low,high,mid-1);
}
}
}
麻烦帮我小小的改动一下 能不能再原基础上 展开
int RecorBinarySearch(int a[],int,int,int,int,int,int);
int main(void)
{
int n=0,m=0;int a[10000],b[10000],c[10000];int low;int high;int mid;
printf("");
scanf("%d",&n);
printf("");
scanf("%d",&m);
printf("");
int i=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("");
for(i=0;i<m;i++)
{
scanf("%d",&b);
c[i]=RecorBinarySearch(a[i],i,n,b,low,high,mid);
}
printf("");
for(i=0;i<m;i++)
{
printf("%d",c[i]);
}
freopen("in.in","r",stdin);
freopen("out.out","w",stdout);
return 0;
}
int RecorBinarySearch(int a[],int i,int n,int key,int low,int high,int mid)
{
low=0;high=n-1;mid=low+(high-low)/2;
if(low>high)
return -1;
else
{
if(a[i]>a[mid])
{
low=mid;
RecorBinarySearch(a,i,n,key,low,high,mid+1);
}
if(a[i]==a[mid])
{
return mid;
}
if(a[i]<a[mid])
{
high=mid;
RecorBinarySearch(a,i,n,key,low,high,mid-1);
}
}
}
麻烦帮我小小的改动一下 能不能再原基础上 展开
展开全部
你把
main()
{
"""""
c[i]=RecorBinarySearch(a[i],i,n,b,low,high,mid);
改成
c[i]=RecorBinarySearch(a,b,0,n-1);
}
然后涵数就改成这样
int RecorBinarySearch(int *a , int b , int n0, int n1)
{ int d=(n0+n1)/2;
if (b==a[d]) return d;
if (b>a[d]) n0=d+1;
else n1=d-1;
if (n1==n0)
{ if (b==a[n0]) return n0;
else return -1;
}
if (n1<n0) return -1;
return (RecorBinarySearch(a, b, n0, n1));
}
这涵数程式我测试过, 应该没问题
main()
{
"""""
c[i]=RecorBinarySearch(a[i],i,n,b,low,high,mid);
改成
c[i]=RecorBinarySearch(a,b,0,n-1);
}
然后涵数就改成这样
int RecorBinarySearch(int *a , int b , int n0, int n1)
{ int d=(n0+n1)/2;
if (b==a[d]) return d;
if (b>a[d]) n0=d+1;
else n1=d-1;
if (n1==n0)
{ if (b==a[n0]) return n0;
else return -1;
}
if (n1<n0) return -1;
return (RecorBinarySearch(a, b, n0, n1));
}
这涵数程式我测试过, 应该没问题
来自:求助得到的回答
展开全部
本人直接打出来的,并未在平台上编译运行过,所以可能会有语法错位,还请自行调试更改
#include<stdio.h>
int RecorBinarySearch(int a[], int, int, int);
int main(void)
{
int i=0, n, m, key;
int a[10000], c[10000];
scanf("%d", &n);
scanf("%d", &m);
printf("提示:输入%d个整数且必须按升序排列。\n", n);
for(i=0; i<n; i++){
scanf("%d", &a[i]);
}
printf("提示:输入%d个预查找的数。\n", m);
for(i=0; i<m; i++){
scanf("%d", &key);
c[i] = RecorBinarySearch(a, key, 0, n-1);
}
printf("提示:输出预查找的数在数组中的位置。(-1代表未找到)\n");
for(i=0; i<m; i++) {
printf("%d ", c[i]);
}
return 0;
}
int RecorBinarySearch(int a[], int key, int low, int high)
{
int nRet;
if(low > high)
nRet = -1;
else {
int mid = (high + low) / 2;
if(key == a[mid])
nRet = mid;
else if(key > a[mid])
nRet = RecorBinarySearch(a, key, mid+1, high);
else if(key < a[mid])
nRet = RecorBinarySearch(a, key, low, mid-1);
}
return nRet;
}
#include<stdio.h>
int RecorBinarySearch(int a[], int, int, int);
int main(void)
{
int i=0, n, m, key;
int a[10000], c[10000];
scanf("%d", &n);
scanf("%d", &m);
printf("提示:输入%d个整数且必须按升序排列。\n", n);
for(i=0; i<n; i++){
scanf("%d", &a[i]);
}
printf("提示:输入%d个预查找的数。\n", m);
for(i=0; i<m; i++){
scanf("%d", &key);
c[i] = RecorBinarySearch(a, key, 0, n-1);
}
printf("提示:输出预查找的数在数组中的位置。(-1代表未找到)\n");
for(i=0; i<m; i++) {
printf("%d ", c[i]);
}
return 0;
}
int RecorBinarySearch(int a[], int key, int low, int high)
{
int nRet;
if(low > high)
nRet = -1;
else {
int mid = (high + low) / 2;
if(key == a[mid])
nRet = mid;
else if(key > a[mid])
nRet = RecorBinarySearch(a, key, mid+1, high);
else if(key < a[mid])
nRet = RecorBinarySearch(a, key, low, mid-1);
}
return nRet;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |