把top=1;改成top=0;就可以找出963
#include <stdio.h>
#define N 15
main()
{
int s[N] = {14,25,36,47,58,69,147,258,369,74,85,96,741,852,963}, i, j, temp, n,a=1;
int top, bottle, mid, sign;
char c;
for ( i = 1; i < N; i++ )
for ( j = 0; j < N - i; j++ )
if ( s[j] < s[j + 1] )
{
temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
for ( i = 0; i < N; i++ )
printf ("%d ",s[i]);
printf("\n");
while (a)
{
printf ("input a number:");
scanf ("%d",&n);
top = 0;
bottle = N;
sign = 1;
while ( sign == 1 )
{
mid = ( top + bottle ) / 2;
if ( n > s[0] || n < s[N - 1] )
{
printf ("the number is not exist");
sign = 0;
}
if(n>s[mid])
{ bottle=mid;
mid=(top+bottle)/2;
if(n==s[mid]) { printf ("%d 's number is s[%d]\n",n,mid);
break;}
else if(top==bottle) exit(0);
}
if(n<s[mid])
{ top=mid;
mid=(top+bottle)/2;
if(n==s[mid]) { printf ("%d 's number is s[%d]\n",n,mid);
break;}
else if(top==bottle) exit(0);
}
}
printf ("continue? 1/0: ");
scanf ("%d",&a);
}
}
#include <stdio.h>
#define N 15
main()
{
int s[N] = {14,25,36,47,58,69,147,258,369,74,85,96,741,852,963}, i, j, temp, n,a=1;
int top, bottle, mid, sign;
char c;
for ( i = 1; i < N; i++ )
for ( j = 0; j < N - i; j++ )
if ( s[j] < s[j + 1] )
{
temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
for ( i = 0; i < N; i++ )
printf ("%d ",s[i]);
printf("\n");
while (a)
{
printf ("input a number:");
scanf ("%d",&n);
top = 0;
bottle = N;
sign = 1;
while ( sign == 1 )
{
mid = ( top + bottle ) / 2;
if ( n > s[0] || n < s[N - 1] )
{
printf ("the number is not exist");
sign = 0;
}
if(n>s[mid])
{ bottle=mid;
mid=(top+bottle)/2;
if(n==s[mid]) { printf ("%d 's number is s[%d]\n",n,mid);
break;}
else if(top==bottle) exit(0);
}
if(n<s[mid])
{ top=mid;
mid=(top+bottle)/2;
if(n==s[mid]) { printf ("%d 's number is s[%d]\n",n,mid);
break;}
else if(top==bottle) exit(0);
}
}
printf ("continue? 1/0: ");
scanf ("%d",&a);
}
}