Projekt

Allgemein

Profil

Feature #644 » main.cpp

Maximilian Seesslen, 22.09.2025 13:06

 
1
#include <stdio.h>
2
#include <time.h>
3
#include <termios.h>
4
#include <errno.h>
5
#include <fcntl.h>
6
#include <QList>
7

    
8

    
9
int size=0x1000000;
10

    
11
int distance(int front, int back)
12
{
13
   if( front > back )
14
   {
15
      return(size-(front-back));
16
   }
17
   return( back-front );
18
}
19

    
20
void right(int& front, int&back, int mid)
21
{
22
   front=mid;
23
}
24

    
25
void left(int& front, int&back, int mid)
26
{
27
   back=mid;
28
}
29

    
30
int steps=0;
31
int search(int front, int back, int value)
32
{
33
   steps=0;
34
   int mid;
35

    
36
   while( distance(front, back) > 1 )
37
   {
38
      mid=(front+(distance(front,back)/2)) % size;
39
      if(mid<value)
40
      {
41
         right(front, back, mid);
42
      }
43
      else
44
      {
45
         left(front, back, mid);
46
      }
47
      steps++;
48
   }
49

    
50
   printf("   Front: 0x%X; Back: 0x%X; distance: %d; value: 0x%X\n", front, back, distance(front, back), value );
51
   if( front == value )
52
   {
53
      printf("   F\n");
54
      return( front );
55
   }
56

    
57
   printf("   B\n");
58
   return( back );
59
}
60

    
61

    
62
int main() {
63

    
64

    
65
   int front=0;
66
   int back=size-1;
67
   int found=search(front, back, 0x12000);
68
   printf("1. Result: 0x%X; steps: %d\n", found, steps);
69

    
70
   front++;
71
   found=search(front, back, 0x12000);
72
   printf("2. Result: 0x%X; steps: %d\n", found, steps);
73

    
74
   front++;
75
   found=search(front, back, 0x12001);
76
   printf("3. Result: 0x%X; steps: %d\n", found, steps);
77

    
78
   front=size-0x100;
79
   back=0x100;
80
   found=search(front, back, 0x12001);
81
   printf("3. Result: 0x%X; steps: %d\n", found, steps);
82

    
83
   return 0;
84
}
    (1-1/1)