- Notifications
You must be signed in to change notification settings - Fork 117
/
Copy path278.c
42 lines (34 loc) · 807 Bytes
/
278.c
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
#include<stdio.h>
#include<stdbool.h>
#include<assert.h>
#defineBAD 3 // for testing
// Forward declaration of isBadVersion API.
boolisBadVersion(intversion) {
return (version >= BAD);
}
intfirstBadVersion(intn) {
if (n <= 1) returnn;
intlow=1;
inthigh=n;
intmid;
while (low <= high) {
mid=low+ (high-low) / 2;
if (isBadVersion(mid)) {
high=mid-1;
}
else {
low=mid+1;
}
}
returnlow;
}
intmain() {
assert(firstBadVersion(3) ==3);
assert(firstBadVersion(4) ==3);
assert(firstBadVersion(5) ==3);
assert(firstBadVersion(6) ==3);
assert(firstBadVersion(7) ==3);
assert(firstBadVersion(8) ==3);
assert(firstBadVersion(9) ==3);
return0;
}