折半查找,输入问题
要求输入数据必须以空格间隔,换行结束#include<stdio.h>
#include<string.h>
void sort(char a[],int n)
{
int i,j;
char temp;
for( i=0;i < n-1;i++)
{
for( j=i+1;j<n;j++)
{
if(a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
int BinSearch(char a[],char ch,int n)
{
int low,high,mid;
low = 0;
high = n-1;
while(low <= high)
{
mid= low +(high-low)/2;printf("%d ",mid);
if(ch > a[mid])
low = mid+1;
else if(ch < a[mid])
high = mid-1;
else
return mid;
}
return -1;
}
int main()
{
char a[80];
char ch;
int k=0;
fgets(a,80,stdin);//这里怎么改,才能实现输入空格间隔,换行结束,只差这一点点了,最好是可以把代码写出来
scanf("%c",&ch);
sort(a,strlen(a));
k=BinSearch(a,ch,strlen(a));printf("\n");
if( k != -1)
printf("%d\n",k);
else
printf("0");
return 0;
}