그닥 나쁘지는 않지만, 컴퓨터공학 박사들이 구현해놓은 코드보다는 허접하겠지뭐.
1 #include <stdio.h>
2
3
4 #define TARGET_NUM 3
5
6 int main(int argc, char * argv[])
7 {
8
9 int arr[] = {0, 1, 2, 3, 5, 7, 9, 12, 21, 23, 27, 40, 41, 50, 69};
10 int arr_n = sizeof(arr)/sizeof(int);
11 int c = arr_n / 2;
12 int t = TARGET_NUM;
13 int found = 0;
14 int try_c = 0;
15 printf("tot %d\n", arr_n);
16 #define TEST
17 #ifdef TEST
18
19 while(1) {
20 printf("try_c %d c = %d [%d]\n", try_c, c, arr[c]);
21 if(c < 0 || c >= arr_n) {
22 break;
23 }
24 try_c++;
25
26
27 if(t == arr[c]) {
28 found = 1;
29 break;
30 }
31
32 if(t < arr[c]) {
33 c = c / 2;
34 continue;
35 }
36
37 if(t > arr[c]) {
38 c = (arr_n + c) / 2;
39 continue;
40 }
41 }
42
43 if(found) {
44 printf("Found %d\n", c);
45 }
46 else {
47 printf("Not found\n");
48 }
49
50 #endif
51 return 0;
52 }
댓글 없음:
댓글 쓰기