请问可以用函数调用的方法解吗?
拜托了!
#include<stdio.h>
int sort(int a[],int first,int last,int x)
{
int mid;
if (first<=last)
{
mid=(first+last)/2;
if (a[mid]==x)
return(mid);
else
if (a[mid]>x)
return(sort(a,mid+1,last,x));
else
return(sort(a,first,mid-1,x));
}
return(-1);
}
int main()
{
int a[15]={100,97,91,85,76,71,66,56,45,34,24,19,15,9,1};
int x;
printf("input the number you want to search\n");
scanf("%d",&x);
if (sort(a,0,14,x)==-1)
printf("the number is not in the array\n");
else
printf("The number is No.%d\n",sort(a,0,14,x)+1);
return 0;
}
#include<stdio.h>
int sort(int a[],int first,int last,int x)
{
int mid;
if (first<=last)
{
mid=(first+last)/2; //判断中间值与x大小关系
if (a[mid]==x) //=则找到
return(mid);
else
if (a[mid]>x) //>则在后半段找,递归
return(sort(a,mid+1,last,x));
else //<则在前半段找,递归
return(sort(a,first,mid-1,x));
}
return(-1);
}
int main()
{
int a[15]={100,97,91,85,76,71,66,56,45,34,24,19,15,9,1};
int x;
printf("input the number you want to search\n");
scanf("%d",&x);
if (sort(a,0,14,x)==-1)
printf("the number is not in the array\n");
else
printf("The number is No.%d\n",sort(a,0,14,x)+1);
return 0;
}