-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest113.c
More file actions
54 lines (52 loc) · 759 Bytes
/
test113.c
File metadata and controls
54 lines (52 loc) · 759 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <stdlib.h>
int randomInt(int n)
{
return rand()%n;
}
int main()
{
int a[32],i,j,tmp,nX = -1, R = 32, L = 0, X, c,stepcount = 0;
srand(time(NULL));
for(i = 0; i < 32; i++)
a[i] = randomInt(101);
for(i = 0; i < 31; i++)
{
for(j = 0; j < 31-i; j++)
{
if (a[j] < a[j+1])
{
tmp = a[i];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
}
printf("please input a number : ");
scanf("%d",&X);
while (R >= L)
{
c = (R + L) / 2;
if (X == a[c])
{
nX = c;
break;
}
if (X > a[c])
{
R = c - 1;
stepcount++;
}
if (X < a[c])
{
L = c + 1;
stepcount++;
}
}
if(nX < 0)
printf("Not found!\n");
else
printf("a[%d] = %d\n",nX,X);
printf("stepcount = %d\n",stepcount);
return 0;
}